Beruflich Dokumente
Kultur Dokumente
DML Command
This command deletes only the
rows from the table based on the
condition given in the where
clause or
Deletes all the rows from the
table if no condition is specified.
But it does not free the space
containing the table
Delete removes content of a
database.
Drop
DDL Command
It removes the all the records or
rows or information along with
structure. It also removes all
information about the table from
data dictionary.
Truncate
DDL Command
This command is used to delete
all the rows or information of
records from the table and free
the space containing the table.
It cant be Rollback
e.g- create table Employee(empid number (5), empname varchar2(10), address varchar2(30));
OR
VALUES
(expression1, expression2, ... );
Or the syntax for the SQL INSERT statement when inserting multiple records using a SELECT statement
is:
INSERT INTO table
(column1, column2, ... )
SELECT expression1, expression2, ...
FROM source_tables
[WHERE conditions];
Parameters or Arguments
table
The table in which to insert the records.
column1, column2
These are the columns in the table to insert values.
expression1, expression2
These are the values to assign to the columns in the table. So column1 would be assigned the
value of expression1,column2 would be assigned the value of expression2, and so on.
source_tables
Used when inserting records from another table. This is the source table when performing the
insert.
WHERE conditions
Optional. Used when inserting records from another table. These are the conditions that must be
met for the records to be inserted.
NOTE
When inserting records into a table using the SQL INSERT statement, you must provide a value
for every NOT NULL column.
You can omit a column from the SQL INSERT statement if the column allows NULL values.
into emp (EmpID, name, Mob_Num, Email_ID, Location, Joining_Date, salary) values (1002,
'santosh', 1234567890, 'san@gmail.com', 'bangalore', '25-sep-2014', 12000)
into emp (EmpID, name, Mob_Num, Email_ID, Location, Joining_Date, salary) values (1003,
'abhay', 1324567890, 'abhay@gmail.com', 'pune', '10-feb-2015', 16000)
into emp (EmpID, name, Mob_Num, Email_ID, Location, Joining_Date, salary) values(1004,
'amit', 3214567890, 'abhay@hotmail.com', 'pune', '6-jan-2015', 180000)
into emp (EmpID, name, Mob_Num, Email_ID, Location, Joining_Date, salary) values (1005,
'dewashish', 5432167890, 'dewa@hotmail.com', 'bangolre', '18-march-2014', 17000)
select * from dual;
EXAMPLE - USING VALUES KEYWORD
Let's look at an example showing how to use the SQL INSERT statement. The simplest way use the
INSERT statement is to insert one record into a table using the VALUES keyword.
For example:
INSERT INTO suppliers
(supplier_id, supplier_name)
VALUES
(24553, 'IBM');
This INSERT statement example would insert one record into the suppliers table. This new record would
have a supplier_idof 24553 and a supplier_name of IBM.
EXAMPLE - USING SELECT STATEMENT
You can also create more complicated SQL INSERT statements using SELECT statement.
For example:
INSERT INTO suppliers
(supplier_id, supplier_name)
SELECT account_no, name
FROM customers
WHERE city = 'Newark';
By placing a SELECT statement within the INSERT statement, you can perform multiples inserts quickly.
With this type of insert, you may wish to check for the number of rows being inserted. You can determine
the number of rows that will be inserted by running the following SQL SELECT
statement before performing the insert.
SELECT count(*)
FROM customers
WHERE city = 'Newark';
The use of the dual table allows you to enter your values in a select statement, even though the values are
not currently stored in a table.
PRACTICE EXERCISE #1:
Based on the employees table, insert an employee record whose employee_number is
1001, employee_name is Sally Johnson and salary is $32,000:
CREATE TABLE employees
( employee_number number(10) not null,
employee_name varchar2(50) not null,
salary number(6),
CONSTRAINT employees_pk PRIMARY KEY (employee_number)
);
Syntax- Insert into <table name> (col1, col2, col3,.....) values(val1, val2, 'val3',......);
Note- To insert date- Use to_date(DD-MM-YYYY)
e.g- insert into employee
(employeeid, firstname, lastname, salary, joining, department)
values(1001, 'john', 'jam', 5000, to_date('1-jan-2013'), 'banking');
Parameters or Arguments
column1, column2
The columns that you wish to update.
expression1, expression2
These are the new values to assign to the column1, column2. So column1 would be assigned the
value of expression1,column2 would be assigned the value of expression2, and so on.
WHERE conditions
Optional. The conditions that must be met for the update to execute. If no conditions are
provided, then all records in the table will be updated.
EXAMPLE - UPDATE SINGLE COLUMN
Let's look at an example showing how to use the SQL UPDATE statement to update a single column in a
table.
UPDATE suppliers
SET supplier_id = 50001
WHERE supplier_name = 'Apple';
This SQL UPDATE example would update the supplier_id to 50001 in the suppliers table where
the supplier_name is 'Apple'.
EXAMPLE - UPDATE MULTIPLE COLUMNS
Let's look at an UPDATE example that shows how to update more than one column in a table.
UPDATE suppliers
SET supplier_name = 'Apple',
product = 'iPhone'
WHERE supplier_name = 'RIM';
When you wish to update multiple columns, you can do this by separating the column/value pairs with
commas.
This SQL UPDATE statement example would update the supplier_name to "Apple" and product to
"iPhone" where thesupplier_name is "RIM".
EXAMPLE - UPDATE TABLE WITH DATA FROM ANOTHER TABLE
Let's look at an UPDATE example that shows how to update a table with data from another table.
UPDATE customers
SET c_details = (SELECT contract_date
FROM suppliers
WHERE suppliers.supplier_name = customers.customer_name)
WHERE customer_id < 1000;
This UPDATE example would update only the customers table for all records where the customer_id is
less than 1000. When the supplier_name from the suppliers table matches the customer_name from
the customers table, the contract_datefrom the suppliers table would be copied to the c_details field in
the customers table.
EXAMPLE - UPDATE MULTIPLE TABLES
Let's look at an UPDATE example that shows how to update multiple tables in an UPDATE statement.
(Please note that this syntax is not valid in Oracle).
UPDATE suppliers, contacts
SET suppliers.status = 'Active',
contacts.note = 'Also Supplier'
WHERE suppliers.supplier_id = contacts.contact_id;
This UPDATE example would update columns in both the suppliers and contacts tables. When
the supplier_id matches thecontact_id, the status column in the suppliers table would be updated to
'Active' and the note column in the contacts table would be updated to 'Also Supplier'.
EXAMPLE - USING EXISTS CLAUSE
You can also perform more complicated updates using the UPDATE statement.
You may wish to update records in one table based on values in another table. Since you can't list more
than one table in the SQL UPDATE statement, you can use the SQL EXISTS clause.
For example:
UPDATE suppliers
SUPPLIER_NAME
CITY
5001
Microsoft
New York
5002
IBM
Chicago
5003
Red Hat
Detroit
5004
NVIDIA
Santa Clara
SUPPLIER_NAME
CITY
5001
Microsoft
San Francisco
5002
IBM
Toronto
5003
Red Hat
Newark
5004
NVIDIA
LA
[WHERE conditions];
Parameters or Arguments
table
The table that you wish to delete records from.
WHERE conditions
Optional. The conditions that must be met for the records to be deleted. If no conditions are
provided, all records in the table will be deleted.
NOTE
You do not need to list fields in the DELETE statement since you are deleting the entire row from
the table.
FROM customers
WHERE customers.customer_id = suppliers.supplier_id
AND customers.customer_name = 'IBM' );
old_city varchar2(50),
status varchar2(20),
CONSTRAINT old_customers_pk PRIMARY KEY (old_customer_id)
);
Parameters or Arguments
expressions
The columns or calculations that you wish to retrieve.
tables
The tables that you wish to retrieve records from. There must be at least one table listed in the
FROM clause.
WHERE conditions
Optional. The conditions that must be met for the records to be selected. If no conditions are
provided, then all records will be selected.
EXAMPLE - SELECT ALL FIELDS FROM ONE TABLE
Let's look at an example showing how to use the SQL SELECT statement to select all fields from a table.
SELECT *
FROM suppliers
WHERE city = 'Newark'
ORDER BY city DESC;
In this SQL SELECT statement example, we've used * to signify that we wish to view all fields from the
suppliers table where the supplier resides in Newark. The result set is sorted by city in descending order.
EXAMPLE - SELECT INDIVIDUAL FIELDS FROM ONE TABLE
You can also use the SQL SELECT statement to select individual fields from the table, as opposed to all
fields from the table.
For example:
SELECT supplier_name, city, state
FROM suppliers
WHERE supplier_id > 1000
ORDER BY name ASC, city DESC;
This SQL SELECT example would return only the supplier_name, city, and state fields from
the suppliers table where thesupplier_id value is greater than 1000. The results are sorted by
supplier_name in ascending order and then city in descending order.
EXAMPLE - SELECT FIELDS FROM MULTIPLE TABLES
You can also use the SQL SELECT statement to retrieve fields from multiple tables.
SELECT orders.order_id, suppliers.name
FROM suppliers
INNER JOIN orders
ON suppliers.supplier_id = orders.supplier_id
ORDER BY order_id;
This SQL SELECT example joins two tables together to gives us a result set that displays the order_id
and supplier name fields where the supplier_id value existed in both the suppliers and orders table. The
results are sorted by order_id in ascending order.
Learn more about SQL joins.
PRACTICE EXERCISE #1:
Based on the employees table below, select all fields from the employees table whose salary is less than or
equal to $52,500 (no sorting is required):
CREATE TABLE employees
( employee_number number(10) not null,
employee_name varchar2(50) not null,
salary number(6),
CONSTRAINT employees_pk PRIMARY KEY (employee_number)
);
state varchar2(25),
CONSTRAINT suppliers_pk PRIMARY KEY (supplier_id)
);
);
ALIASES- SQL ALIASES can be used to create a temporary name for columns or
tables.
COLUMN ALIASES are used to make column headings in your result set easier
to read.
TABLE ALIASES are used to shorten your SQL to make it easier to read or
when you are performing a self join (ie: listing the same table more than once in
the FROM clause).
SYNTAX
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]
UNION
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];
NOTE
The corresponding expressions must have the same data type in the SELECT
statements. For example: expression1must be the same data type in both the first and
second SELECT statement.
For example:
SELECT supplier_id
FROM suppliers
UNION
SELECT supplier_id
FROM orders
ORDER BY supplier_id;
If you had the suppliers table populated with the following records:
supplier_id
supplier_name
1000
Microsoft
2000
Oracle
3000
Apple
4000
Samsung
order_id
order_date
supplier_id
2015-08-01
2000
2015-08-01
6000
2015-08-02
7000
2015-08-03
8000
SELECT supplier_id
FROM suppliers
UNION
SELECT supplier_id
FROM orders
ORDER BY supplier_id;
supplier_id
1000
2000
3000
4000
6000
7000
8000
combine the result sets of 2 or more SELECT statements. It does not remove duplicate
rows between the various SELECT statements (all rows are returned).
Each SELECT statement within the UNION ALL must have the same number of fields in
the result sets with similar data types.
E.GSELECT supplier_id
FROM suppliers
UNION ALL
SELECT supplier_id
FROM orders
ORDER BY supplier_id;
INTERSECT OPERATOR-
return the results of 2 or more SELECT statements. However, it only returns the rows
selected by all queries or data sets. If a record exists in one query and not in the other, it
will be omitted from the INTERSECT results.
Explanation: The INTERSECT query will return the records in the blue shaded area.
These are the records that exist in both Dataset1 and Dataset2.
Each SQL statement within the SQL INTERSECT must have the same number of fields
in the result sets with similar data types.
SYNTAX
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]
INTERSECT
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];
Example- Slect name from cricket_team
Intersect
Slect name from football_team;
MINUS OPERATORThe SQL MINUS operator is used to return all rows in the first SELECT statement that
are not returned by the second SELECT statement. Each SELECT statement will define
a dataset. The MINUS operator will retrieve all records from the first dataset and then
remove from the results all records from the second dataset.
Minus Query
Explanation: The MINUS query will return the records in the blue shaded area. These
are the records that exist in Dataset1 and not in Dataset2.
Each SELECT statement within the MINUS query must have the same number of fields
in the result sets with similar data types.
TIP: The MINUS operator is not supported in all SQL databases. It can used in
databases such as Oracle.
For databases such as SQL Server, PostgreSQL, and SQLite, use the EXCEPT
operator to perform this type of query.
SYNTAX
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]
MINUS
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];
Difference B/W Minus & IntersectMinus- Eliminate the common rows or record from the result set.
Intersect- Returns common rows or records from the result set.
WHERE CLAUSEThe Oracle WHERE clause is used to filter the results from
a SELECT, INSERT, UPDATE, or DELETE statement.
SYNTAX
WHERE conditions;
In this Oracle WHERE clause example, we've used the WHERE clause to filter our
results from the customers table. The SELECT statement above would return all rows
from the customers table where the last_name is Anderson. Because the * is used in the
SELECT, all fields from the customers table would appear in the result set.
GROUP BY CLAUSE
The Oracle GROUP BY clause is used in a SELECT statement to collect data across
multiple records and group the results by one or more columns.
SYNTAX
SELECT expression1, expression2, ... expression_n,
aggregate_function (aggregate_expression)
FROM tables
[WHERE conditions]
Because you have listed one column (the product field) in your SELECT statement that
is not encapsulated in the SUM function, you must use the GROUP BY clause.
The product field must, therefore, be listed in the GROUP BY clause.
HAVING CLAUSE
The Oracle HAVING clause is used in combination with the GROUP BY clause to
restrict the groups of returned rows to only those whose the condition is TRUE.
SYNTAX
SELECT expression1, expression2, ... expression_n,
aggregate_function (aggregate_expression)
FROM tables
[WHERE conditions]
GROUP BY expression1, expression2, ... expression_n
HAVING having_condition;
Parameters or Arguments
expression1, expression2, ... expression_n
The expressions that are not encapsulated within an aggregate function and must be
included in the GROUP BY clause.
aggregate_function
It can be a function such as SUM, COUNT, MIN, MAX, or AVG functions.
aggregate_expression
This is the column or expression that the aggregate_function will be used against.
tables
The tables that you wish to retrieve records from. There must be at least one table listed
in the FROM clause.
WHERE conditions
Optional. These are the conditions for the records to be selected.
having_condition
This is a further condition applied only to the aggregated results to restrict the groups of
returned rows. Only those groups whose condition evaluates to TRUE will be included in
the result set.
Let's look at how we could use the HAVING clause with the COUNT function.
You could use the COUNT function to return the name of the department and the
number of employees (in the associated department) that make under $49,500 / year.
The Oracle HAVING clause will filter the results so that only departments with more than
10 employees will be returned.
SELECT department, COUNT(*) AS "Number of employees"
FROM employees
WHERE salary < 49500
GROUP BY department
HAVING COUNT(*) > 10;
For example, you could also use the MAX function to return the name of each
department and the maximum salary in the department. The Oracle HAVING clause will
return only those departments whose maximum salary is greater than $45,000.
SELECT department, MAX(salary) AS "Highest salary"
FROM employees
GROUP BY department
HAVING MAX(salary) > 45000;
DISTINCT CLAUSE
The Oracle DISTINCT clause is used to remove duplicates from the result set. The
DISTINCT clause can only be used withSELECT statements.
SYNTAX
SELECT DISTINCT expressions
FROM tables
[WHERE conditions];
NOTE
When only one expression is provided in the DISTINCT clause, the query will return the
unique values for that expression.
When more than one expression is provided in the DISTINCT clause, the query will
retrieve unique combinations for the expressions listed.
In Oracle, the DISTINCT clause doesn't ignore NULL values. So when using the
DISTINCT clause in your SQL statement, your result set will include NULL as a distinct
value.
FROM customers
WHERE last_name = 'Smith';
This Oracle DISTINCT example would return all unique state values from
the customers table where the customer'slast_name is 'Smith'.
ORDER BY CLAUSE
The SQL ORDER BY clause is used to sort the records in the result set for a SELECT
statement.
SYNTAX
SELECT expressions
FROM tables
[WHERE conditions]
NOTE
If the ASC or DESC modifier is not provided in the ORDER BY clause, the results will be
sorted by expression in ascending order. This is equivalent to ORDER
BY expression ASC.
This SQL ORDER BY example would return all records sorted by the supplier_city field
in ascending order and would be equivalent to the following SQL ORDER BY clause:
SELECT supplier_city
FROM suppliers
WHERE supplier_name = 'IBM'
ORDER BY supplier_city ASC;
SELECT supplier_city
FROM suppliers
WHERE supplier_name = 'IBM'
ORDER BY supplier_city DESC;
This SQL ORDER BY example would return all records sorted by the supplier_city field
in descending order.
This SQL ORDER BY would return all records sorted by the supplier_city field in
descending order, since the supplier_city field is in position #1 in the result set and
would be equivalent to the following SQL ORDER BY clause:
SELECT supplier_city
FROM suppliers
WHERE supplier_name = 'IBM'
ORDER BY supplier_city DESC;
For example:
SELECT supplier_city, supplier_state
FROM suppliers
WHERE supplier_name = 'IBM'
ORDER BY supplier_city DESC, supplier_state ASC;
This SQL ORDER BY would return all records sorted by the supplier_city field in
descending order, with a secondary sort by supplier_state in ascending order.
RANK FUNCTION
The Oracle/PLSQL RANK function returns the rank of a value in a group of values. It is
very similar to the DENSE_RANK function. However, the rank function can cause nonconsecutive rankings if the tested values are the same. Whereas, theDENSE_RANK
function will always result in consecutive rankings.
The RANK function can be used two ways - as an Aggregate function or as an Analytic
function.
NOTE
There must be the same number of expressions in the first expression list as there is in
the ORDER BY clause.
The expression lists match by position so the data types must be compatible between
the expressions in the first expression list as in the ORDER BY clause.
APPLIES TO
The RANK function can be used in the following versions of Oracle/PLSQL:
The SQL statement above would return the rank of an employee with a salary of $1,000
and a bonus of $500 from within the employees table.
APPLIES TO
The RANK function can be used in the following versions of Oracle/PLSQL:
The SQL statement above would return all employees who work in the Marketing
department and then calculate a rank for each unique salary in the Marketing
department. If two employees had the same salary, the RANK function would return the
same rank for both employees. However, this will cause a gap in the ranks (ie: nonconsecutive ranks). This is quite different from the DENSE_RANK function which
generates consecutive rankings.
DENSE_RANK FUNCTION
The Oracle/PLSQL DENSE_RANK function returns the rank of a row in a group of rows.
It is very similar to the RANK function. However, the RANK function can cause nonconsecutive rankings if the tested values are the same. Whereas, the DENSE_RANK
function will always result in consecutive rankings.
The DENSE_RANK function can be used two ways - as an Aggregate function or as an
Analytic function.
As an Aggregate function, the DENSE_RANK function returns the dense rank of a row
within a group of rows.
The syntax for the DENSE_RANK function when used as an Aggregate function is:
DENSE_RANK( expression1, ... expression_n ) WITHIN GROUP ( ORDER BY
expression1, ... expression_n )
Parameters or Arguments
expression1 .. expression_n
One or more expressions which identify a unique row in the group.
NOTE
There must be the same number of expressions in the first expression list as there is in
the ORDER BY clause.
The expression lists match by position so the data types must be compatible between
the expressions in the first expression list as in the ORDER BY clause.
APPLIES TO
The DENSE_RANK function can be used in the following versions of Oracle/PLSQL:
The SQL statement above would return the dense rank of an employee with a salary of
$1,000 and a bonus of $500 from within the employees table.
APPLIES TO
The DENSE_RANK function can be used in the following versions of Oracle/PLSQL:
The SQL statement above would return all employees who work in the Marketing
department and then calculate a rank for each unique salary in the Marketing
department. If two employees had the same salary, the DENSE_RANK function would
return the same rank for both employees.