SQL Exerice
SQL Exerice
Syntax : The syntax for the Oracle CREATE TABLE statement is:
**********************************************************
Create Table Example 5 :
This Oracle ALTER TABLE example will add two columns, customer_name as a varchar2(45) field and
city as a varchar2(40) field to the customers table.
This Oracle ALTER TABLE example will modify the column called customer_name to be a data type of
varchar2(100) and force the column to not allow null values.
**********************************************************
Alter Table Example 4 :
This Oracle ALTER TABLE example will modify both the customer_name and city columns.
RENAME TO new_table_name;
Syntax :
**********************************************************
**********************************************************
Description :The Oracle DROP TABLE statement allows you to remove or delete a
table from the Oracle database.
[ CASCADE CONSTRAINTS ];
Where : schema_name is the name of the schema that owns the table.
table_name is the name of the table to remove from the Oracle database.
CASCADE CONSTRAINTS is optional. If specified, all referential integrity constraints will be dropped as
well.
Insert Example 1:
UPDATE table
SET column1 = expression1,
column2 = expression2,
...
WHERE conditions;
Where : column1, column2 are the columns that you wish to update.
Update Example 1:
UPDATE customers
SET last_name = 'Anderson'
WHERE customer_id = 5000;
**********************************************************
Update Example 2:
UPDATE customers
SET state = 'California',
customer_rep = 32
WHERE customer_id > 100;
7. DELETE Statement
Description :The Oracle DELETE statement is used to delete a single record or
multiple records from a table in Oracle.
Where : table is the table that you wish to delete records from and conditions are
conditions that must be met for the records to be deleted.
Delete example 1:
SELECT expressions
FROM tables
WHERE conditions;
Where : expressions are the columns or calculations that you wish to retrieve. Tables are the tables
that you wish to retrieve records from. There must be at least one table listed in the FROM clause.
conditions are conditions that must be met for the records to be selected.
Select Example 1 :
SELECT *
FROM contacts
WHERE last_name = 'Smith'
AND contact_id >= 1000
AND contact_id <= 2000;
**********************************************************
Select Example 2 :
SELECT *
FROM contacts
WHERE last_name = 'Smith'
AND contact_id BETWEEN 1000 AND 2000;
**********************************************************
Select Example 3 :
Where : expression1, expression2, ... expression_n are expressions that are not encapsulated within
an aggregate function and must be included in the GROUP BY Clause. Aaggregate_function can be
a function such as SUM, COUNT, MIN, MAX, or AVG functions. tables are the tables that you
wish to retrieve records from. There must be at least one table listed in the FROM clause.
conditions are conditions that must be met for the records to be selected.
Select Example 5 :
**********************************************************
Select Example 6 :
**********************************************************
Select Example 7 :
**********************************************************
Select Example 8 :
aggregate_function (expression)
FROM tables
WHERE conditions
GROUP BY expression1, expression2, ... expression_n
HAVING condition;
**********************************************************
Select Example 8 :
SELECT *
FROM customers
WHERE customer_name IN ('IBM', 'Hewlett Packard', 'Microsoft');
**********************************************************
Select Example 11 :
FROM customers
WHERE customer_name = 'IBM'
OR customer_name = 'Hewlett Packard'
OR customer_name = 'Microsoft';
**********************************************************
Select Example 12 :
SELECT *
FROM orders
WHERE order_id IN (10000, 10001, 10003, 10005);
SELECT *
FROM orders
WHERE order_id = 10000
OR order_id = 10001
OR order_id = 10003
OR order_id = 10005;
**********************************************************
Select Example 13 :
SELECT *
FROM customers
WHERE customer_name NOT IN ( 'IBM', 'Hewlett Packard', 'Microsoft');
**********************************************************
8.4 Subqueries
SELECT *
FROM all_tables tabs
WHERE tabs.table_name IN (SELECT cols.table_name
FROM all_tab_columns cols
WHERE cols.column_name = 'SUPPLIER_ID');
**********************************************************
SQL ORDER BY
The ORDER BY keyword is used to sort the result-set by one or more columns. The ORDER
BY keyword sorts the records in ascending order by default. To sort the records in a
descending order, you can use the DESC keyword.
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;
or:
SELECT column_name(s)
FROM table1
LEFT OUTER JOIN table2
ON table1.column_name=table2.column_name;
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name=table2.column_name;
or:
SELECT column_name(s)
FROM table1
RIGHT OUTER JOIN table2
ON table1.column_name=table2.column_name;
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;
The COUNT() function returns the number of rows that matches a specified criteria. The
COUNT(column_name) function returns the number of values (NULL values will not be
counted) of the specified column:
SELECT COUNT(column_name) FROM table_name;
SELECT ProductName,UnitPrice*(UnitsInStock+UnitsOnOrder)
FROM Products
SELECT ProductName,UnitPrice*(UnitsInStock+NVL(UnitsOnOrder,0))
FROM Products