Adding data to a table -The INSERT statement




Can also be used to add a row to a view
Syntax:
      INSERT INTO <tablen...
Data can be added only to some columns in a row
by specifying the columns and their data.


Example:
INSERT INTO Employee ...
MG/DB CONCEPTS/CL12
Example:

INSERT INTO Employee VALUES (1001,
'Ram', 'Admin', 'Typist', ‘10-MAR-04’);


  The insert statement adds a new r...
View data in the table - The Select statement




                                    MG/DB CONCEPTS/CL12
The select command displays information
from the table as a set of records.
Syntax:
SELECT [DISTINCT/ALL] {* FROM
<table n...
Three keywords make up a basic select statement:
SELECT, FROM AND WHERE.
  The SELECT statement retrieves rows from the
ta...
Reordering columns in query results
   In the select query, the column names can be
specified in any order.
  The output w...
Selecting data from all the rows using the
keyword ALL
The keyword ALL retains duplicate rows from
the results of a SELECT...
Eliminating redundant data using the keyword
 DISTINCT
   The distinct keyword eliminates duplicate rows from
the results ...
Changing column headings
   By default, the column heading displayed in the query
results is the column name designated wh...
MG/DB CONCEPTS/CL12
MG/DB CONCEPTS/CL12
Selecting specific rows – WHERE clause in
SELECT


The WHERE clause in SELECT statement
specifies the search criteria for ...
The condition statement known as the search criteria, in
the WHERE clause can include the following:


Comparison operator...
Choosing rows based on comparisons
  Comparison operators allow you to select rows by
comparing a row to a given value or ...
The valid SQL comparison symbols are:
< Less than        <= Less than or equal
> Greater than     >= Greater than or equal...
WHERE – AND and OR to link conditions
Logical operators can be used to connect search
conditions in a WHERE clause.
SELECT...
WHERE – NOT to reverse condition
NOT can be used to reverse the meaning of an entire WHERE search
condition.
SELECT select...
Choosing rows based on a range(WHERE – BETWEEN)
  The BETWEEN operator can be used to search for a range of
      values i...
Choosing rows based on a list(WHERE – IN)
  To specify a list of values, IN keyword is used.
    The IN operator selects v...
Choosing rows based on character strings (WHERE – LIKE)
 SQL includes a string matching operator, LIKE for comparisons on
...
Wildcard     Description
%          Any string of zero or more characters
_          (underscore) Any single character


S...
The following example searches for all names beginning with A
Ex:   SELECT * FROM Employee WHERE name LIKE 'A%';


NOTE:
 ...
Examples of using wildcards with the LIKE clause:
Expression        returns
LIKE ‘A%’         Every name that begins with ...
Choosing rows based on unknown values(Searching for
NULL)
NULL means that the value for that column is unknown or
unavaila...
SELECT INTO
A SELECT statement with an INTO clause allows you
to define a table and put data into it without going
through...
Syntax:
     SELECT select_list
     INTO new_table_name
     FROM table_name
     WHERE search_conditon
Examples:
SELECT ...
Sorting results – ORDER BY clause
  ORDER BY sorts query results by one or more columns.
  ORDER BY clause is the last cla...
The following example arranges rows by dept; within
each dept, rows are arranged by salary


SELECT empno, name, dept, sal...
Upcoming SlideShare
Loading in...5
×

Sql2

545

Published on

For NPS CLASS 12

Published in: Technology, Economy & Finance
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
545
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Sql2"

  1. 1. Adding data to a table -The INSERT statement Can also be used to add a row to a view Syntax: INSERT INTO <tablename> (column1,column2.,..) VALUES(<value1>,<value2>.,..); MG/DB CONCEPTS/CL12
  2. 2. Data can be added only to some columns in a row by specifying the columns and their data. Example: INSERT INTO Employee (ecode, name, dept) VALUES (2001, 'Rani', 'Accounts'); The columns that are not listed in the INSERT command will have their default value, if it is defined otherwise NULL value. MG/DB CONCEPTS/CL12
  3. 3. MG/DB CONCEPTS/CL12
  4. 4. Example: INSERT INTO Employee VALUES (1001, 'Ram', 'Admin', 'Typist', ‘10-MAR-04’); The insert statement adds a new row to Employee giving a value for every column in the row. The data values must be in the same order as the column names in the table. MG/DB CONCEPTS/CL12
  5. 5. View data in the table - The Select statement MG/DB CONCEPTS/CL12
  6. 6. The select command displays information from the table as a set of records. Syntax: SELECT [DISTINCT/ALL] {* FROM <table name>[,<table name>]} [WHERE clause][GROUP BY clause] [HAVING clause] [ORDER BY clause] ; MG/DB CONCEPTS/CL12
  7. 7. Three keywords make up a basic select statement: SELECT, FROM AND WHERE. The SELECT statement retrieves rows from the table. The FROM clause specifies the table where the columns are located. The WHERE clause specifies the rows. Examples: SELECT * FROM Employee; SELECT empno, name FROM Employee; MG/DB CONCEPTS/CL12
  8. 8. Reordering columns in query results In the select query, the column names can be specified in any order. The output will be in exactly the same order. The order of selection determines the order of display. Example: SELECT Dept, Job_title, Empno, Name FROM Employee; MG/DB CONCEPTS/CL12
  9. 9. Selecting data from all the rows using the keyword ALL The keyword ALL retains duplicate rows from the results of a SELECT statement. Example: SELECT ALL Name FROM Employee; The above statement outputs values of the column name from every row of the table. MG/DB CONCEPTS/CL12
  10. 10. Eliminating redundant data using the keyword DISTINCT The distinct keyword eliminates duplicate rows from the results of a SELECT statement. Example: SELECT DISTINCT Name FROM Employee; In this example, the output will not have duplicate rows. The DISTINCT keyword can be specified only once in a given SELECT clause. If the clause selects multiple fields, DISTINCT eliminates rows, where all of the selected fields are identical. MG/DB CONCEPTS/CL12
  11. 11. Changing column headings By default, the column heading displayed in the query results is the column name designated when the table was created. However, it is possible to change the column heading by including it in the select_list. Changing column heading is referred to as aliasing; this is done to make the results more readable. Example: SELECT empno as emp_number, name as emp_name, doj as date_of_joining from employee; MG/DB CONCEPTS/CL12
  12. 12. MG/DB CONCEPTS/CL12
  13. 13. MG/DB CONCEPTS/CL12
  14. 14. Selecting specific rows – WHERE clause in SELECT The WHERE clause in SELECT statement specifies the search criteria for selecting rows. Syntax: SELECT <column name>[,<column name>] FROM <table name> WHERE <condition>; MG/DB CONCEPTS/CL12
  15. 15. The condition statement known as the search criteria, in the WHERE clause can include the following: Comparison operators <, >, <=, >=, !<, !>, <>, = Ranges BETWEEN and NOT BETWEEN Lists IN and NOT IN String matches LIKE and NOT LIKE Unknown values IS NULL and IS NOT NULL Logical operators AND, OR Negation NOT MG/DB CONCEPTS/CL12
  16. 16. Choosing rows based on comparisons Comparison operators allow you to select rows by comparing a row to a given value or expression. Used optionally in SELECT, UPDATE, and DELETE commands to restrict their operations to the rows specified. SELECT select_list FROM table_list WHERE [NOT] <search condition> [AND/OR [NOT] <search condition>...] Searches for rows WHERE the comparison between the specified expressions is true. MG/DB CONCEPTS/CL12
  17. 17. The valid SQL comparison symbols are: < Less than <= Less than or equal > Greater than >= Greater than or equal !< Not less than !> Not greater than = Equal <> or != or # Not equal Example: SELECT name FROM Employee WHERE salary >= 50000; MG/DB CONCEPTS/CL12
  18. 18. WHERE – AND and OR to link conditions Logical operators can be used to connect search conditions in a WHERE clause. SELECT select_list FROM table_list WHERE <search condition> AND/OR <search_condition>...] If conditions are linked by AND, the WHERE clause is true only if both search conditions are evaluated as true. If conditions are linked by OR, the WHERE clause is true if either search condition is evaluated to be true. Example: SELECT * FROM Employee WHERE dept = 'Admin' OR dept = 'Accounts’ MG/DB CONCEPTS/CL12
  19. 19. WHERE – NOT to reverse condition NOT can be used to reverse the meaning of an entire WHERE search condition. SELECT select_list FROM table_list WHERE NOT <search condition> Example: SELECT name FROM Employee WHERE NOT salary >= 50000; Note: NOT also an option within certain conditions, where it reverses the meaning of the keyword NOT <Comparison conditions> , NOT BETWEEN, NOT LIKE, NOT IN, NOT EXISTS MG/DB CONCEPTS/CL12
  20. 20. Choosing rows based on a range(WHERE – BETWEEN) The BETWEEN operator can be used to search for a range of values in a WHERE clause. The range includes both the lower value and the upper value. SELECT select_list FROM table_list WHERE [NOT] <expression> [NOT] BETWEEN <expression1> AND <expression2> Searches for rows WHERE the specified expression's value falls BETWEEN the two expressions. BETWEEN is equivalent to <expression> >= <expression1> and <expression> <= <expression2> Example: SELECT name from Employee WHERE salary BETWEEN 15000 AND 25000; MG/DB CONCEPTS/CL12
  21. 21. Choosing rows based on a list(WHERE – IN) To specify a list of values, IN keyword is used. The IN operator selects values that match any value in a given list of values. SELECT select_list FROM table_list WHERE [NOT] <expression> [NOT] IN (<valuelist>/<subselect>) Searches for all rows WHERE the specified column's value matches any of the values. These values may be a simple list, or they may be returned by a subselect. Example: SELECT name FROM Employee WHERE dept IN ('Software', 'Admin', 'Sales'); MG/DB CONCEPTS/CL12
  22. 22. Choosing rows based on character strings (WHERE – LIKE) SQL includes a string matching operator, LIKE for comparisons on character strings using patterns. Patterns are described using wildcard characters. MG/DB CONCEPTS/CL12
  23. 23. Wildcard Description % Any string of zero or more characters _ (underscore) Any single character Syntax: SELECT select_list FROM table_list WHERE [NOT] <column name> [NOT] LIKE '<search string>' Searches for rows WHERE the specified column's value matches the search string. MG/DB CONCEPTS/CL12
  24. 24. The following example searches for all names beginning with A Ex: SELECT * FROM Employee WHERE name LIKE 'A%'; NOTE: A string with wildcard characters must always be enclosed in quotation marks. Patterns are case sensitive, i.e, upper case characters do not match lower case characters or vice versa MG/DB CONCEPTS/CL12
  25. 25. Examples of using wildcards with the LIKE clause: Expression returns LIKE ‘A%’ Every name that begins with A followed by any number of characters LIKE ‘Ar__’ Every name that begins with Ar followed by 2 more characters LIKE ‘%ter’ Every name that ends with ter LIKE ‘%ar%’ Every name that has letters ar in it LIKE ‘_om’ Every three-letter name ending with om MG/DB CONCEPTS/CL12
  26. 26. Choosing rows based on unknown values(Searching for NULL) NULL means that the value for that column is unknown or unavailable. To select rows with NULL values, use IS NULL in the WHERE clause. Syntax: SELECT select_list FROM table_list WHERE column_name IS [NOT] NULL Ex: SELECT * FROM Employee WHERE desig IS NULL; Non Null values in a table can be listed by using IS NOT NULL MG/DB CONCEPTS/CL12
  27. 27. SELECT INTO A SELECT statement with an INTO clause allows you to define a table and put data into it without going through the usual data definition process. SELECT INTO creates a new table based in the results of a query. If a table already exists with the same name, this statement will fail. The new table columns and rows are based the select_list specifies in the query. MG/DB CONCEPTS/CL12
  28. 28. Syntax: SELECT select_list INTO new_table_name FROM table_name WHERE search_conditon Examples: SELECT empcode, name INTO temp1 FROM emp; SELECT empno, name INTO temp2 FROM EMP WHERE job_title="Manager"; MG/DB CONCEPTS/CL12
  29. 29. Sorting results – ORDER BY clause ORDER BY sorts query results by one or more columns. ORDER BY clause is the last clause in a SQL statement. It can operate on multiple fields. Syntax: SELECT select_list FROM table_list ORDER BY <column name>/<integer> [ASC/DESC] [,<column name>/<integer> [ASC/DESC]...]; ORDERs the displayed rows by the values in the specified column(s). ASC or DESC specifies whether values will be displayed in ASCending or DESCending order. ASC is the default. You cannot ORDER BY columns of type LOGICAL MG/DB CONCEPTS/CL12
  30. 30. The following example arranges rows by dept; within each dept, rows are arranged by salary SELECT empno, name, dept, salary FROM EMP ORDER BY dept, salary desc; NOTE: ORDER BY cannot be used inside a nested SUB-SELECT. MG/DB CONCEPTS/CL12
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×