Upload
guest319770
View
534
Download
0
Tags:
Embed Size (px)
DESCRIPTION
For NPS CLASS 12
Citation preview
MG/DB CONCEPTS/CL12
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
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
MG/DB CONCEPTS/CL12
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
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 name>[,<table name>]}
[WHERE clause][GROUP BY clause] [HAVING clause] [ORDER BY clause] ;
MG/DB CONCEPTS/CL12
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
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
Selecting data from all the rows using the keyword ALLThe 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
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
Changing column headingsBy 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
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 selecting rows.
Syntax:
SELECT <column name>[,<column name>] FROM <table name> WHERE <condition>;
MG/DB CONCEPTS/CL12
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
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
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
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
WHERE – NOT to reverse conditionNOT can be used to reverse the meaning of an entire WHERE searchcondition.
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
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
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
Choosing rows based on character strings (WHERE – LIKE)
SQL includes a string matching operator, LIKE for comparisons oncharacter strings using patterns. Patterns are described using wildcard characters.
MG/DB CONCEPTS/CL12
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
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
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 Arfollowed 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
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
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
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
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
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.