Professional Documents
Culture Documents
SQL Lab Manual
SQL Lab Manual
& Tech
TOPIC 1
DDL Commands :
Syntax
Example 1
This example demonstrates how you can create a table named "Person",
with four columns. The column names will be "LastName", "FirstName",
"Address", and "Age":
FirstName varchar,
Address varchar,
Age int )
This example demonstrates how you can specify a maximum length for
some columns:
Example 2
Syntax
Syntax
Example
Syntax
Example
a. INSERT COMMAND :
Syntax
Example
Syntax
b. SELECT COMMAND :
Syntax
SELECT A1,A2……..
FROM tablename
WHERE predicate
A1,A2 is the list of attributes and predicate is the condition which must
be satisfied by the resulting rows .
Example 1
SELECT last_name
FROM personal_info
The command shown below retrieves all of the information contained within
the personal_info table. The asterisk is used as a wildcard in SQL. This
means "Select everything from the personal_info table."
Example 2
SELECT *
FROM personal_info
Finally, the WHERE clause can be used to limit the records that are retrieved
to those that meet specified criteria. The CEO might be interested in
reviewing the personnel records of all highly paid employees. The following
command retrieves all of the data contained within personal_info for records
that have a salary value greater than $50,000 :
Example 3
c. UPDATE COMMAND :
Syntax
UPDATE tablename
SET columnname=expression,columnname=expression,…..
WHERE columnname=expression;
Example
UPDATE personal_info
SET salary=salary*1.03
d. DELETE COMMAND :
Syntax
The DELETE command with a WHERE clause can be used to remove his
record from the personal_info table:
Example
The following command deletes all the rows from the table
Example
TOPIC 2
Entity integrity
Domain integrity
Referential integrity
User-defined integrity
ENTITY INTEGRITY :
Entity integrity defines a row as a unique entity for a particular table. Entity
integrity enforces the integrity of the identifier column(s) or the primary key
of a table (through indexes, UNIQUE constraints, PRIMARY KEY constraints,
or IDENTITY properties).
DOMAIN INTEGRITY :
Domain integrity is the validity of entries for a given column. You can
enforce domain integrity by restricting the type (through data types), the
format (through CHECK constraints and rules), or the range of possible
values (through FOREIGN KEY constraints, CHECK constraints, DEFAULT
definitions, NOT NULL definitions, and rules).
REFERENTIAL INTEGRITY :
This constraint ensures that NULL values are not entered in those
columns.
c. UNIQUE CONSTRAINT :
d. CHECK CONSTRAINT :
Syntax
Example
TOPIC 3
3.1 JOINS :
Sometimes we have to select data from two or more tables to make
our result complete. We have to perform a join. Tables in a database can be
related to each other with keys.
In the "Employees" table below, the "Employee_ID" column is the primary
key, meaning that no two rows can have the same Employee_ID. The
Employee_ID distinguishes two persons even if they have the same name.
Employees :
Employee_ID Name
01 Hansen, Ola
02 Svendson, Tove
03 Svendson, Stephen
04 Pettersen, Kari
Orders :
Example
Result
Name Product
Hansen, Ola Printer
Svendson, Stephen Table
Svendson, Stephen Chair
Using Joins
OR we can select data from two tables with the JOIN keyword, like this:
The INNER JOIN returns all rows from both tables where there is a
match. If there are rows in Employees that do not have matches in Orders,
those rows will not be listed.
Example 1
Example 2
Name Product
Hansen, Ola Printer
Svendson, Stephen Table
Svendson, Stephen Chair
The LEFT JOIN returns all the rows from the first table (Employees),
even if there are no matches in the second table (Orders). If there are rows
in Employees that do not have matches in Orders, those rows also will be
listed.
Syntax :
Example :
Result
Name Product
Hansen, Ola Printer
Svendson, Tove
Svendson, Stephen Table
Svendson, Stephen Chair
Pettersen, Kari
The RIGHT JOIN returns all the rows from the second table
(Orders), even if there are no matches in the first table (Employees). If
there had been any rows in Orders that did not have matches in Employees,
those rows also would have been listed.
Syntax :
Example :
Result
Name Product
Hansen, Ola Printer
Svendson, Stephen Table
Svendson, Stephen Chair
TOPIC 4
1. SUM :
Example :
Result :
Total
-----------
1837
2. AVG :
Example :
3. COUNT :
Example :
Above query displays how many orders are processed by company that
requested over 100 widgets.
SQL provides Min and Max functions to locate the records containing
the smallest and largest values for a given expression
The MAX() function returns the largest value in a given data series. We can
provide the function with a field name to return the largest value for a given
field in a table.To find the order in our database that produced the most
revenue for the company,following query will be used:
Example :
FROM WidgetOrders
The MIN() function functions in the same manner, but returns the minimum
value for the expression.
TOPIC 5
1. ABS :
ABS ( numeric_expression )
Example :
SELECT abs(-15.0)
FROM dual
Result: 15
2. POWER :
POWER ( numeric_expression , y )
Example :
SELECT power(3,2)
FROM dual
Result :
3. ROUND :
Example :
SELECT round(15.133,1)
FROM dual
Result: 15.1
4. SQRT : |
SQRT ( float_expression )
Example :
SELECT sqrt(49)
FROM dual
Result : 7
5. EXP :
EXP ( float_expression )
Example :
SELECT exp(4)
FROM dual
Result : 54.59815
6. LOG :
LOG ( base,no )
Example :
SELECT log(10,100)
FROM dual
Result : 2
7. COS :
COS ( float_expression )
Example :
SELECT cos(0)
FROM dual
Result: 1
8. SIN :
SIN ( float_expression )
Example :
SELECT sin(0)
FROM dual
Result: 0
9. TAN :
TAN ( float_expression )
Example :
SELECT tan(0)
FROM dual
Result: 0
10. SIGN :
Returns the positive (+1), zero (0), or negative (-1) sign of the
given expression
SIGN ( numeric_expression)
Example :
SELECT sign(-15)
FROM dual
Result : -1
1 LOWER :
LOWER ( character_expression )
Example :
SELECT lower(‘DYPIET’)
FROM dual
Result: dypiet
2. UPPER :
UPPER ( character_expression )
Example :
SELECT upper(‘dypiet’)
FROM dual
Result: DYPIET
3. REPLACE :
Example :
SELECT replace(‘scott’,’s’,’boy’)
FROM dual;
Result: boycott
4. SUBSTRING :
Example :
SELECT substr(‘daisy’,2,3)
FROM dual;
Result: AIS
5. LENGTH :
LENGTH(string_expression)
Example :
SELECT length(‘daisy’)
FROM dual;
Result: 5
6. CONCATENATION :
Example :
Result: mitaoe
7. INITCAP :
INITCAP(string_expression)
Example :
SELECT initcap(‘college’)
FROM dual;
Result: College
8. USER :
Example :
SELECT user
FROM dual;
Result: SCOTT
1. ADD_MONTHS :
ADD_MONTHS(date,no. of months)
Example :
SELECT add_months(’20-nov-04’,3)
FROM dual;
Result: 20-FEB-05
2. LAST_DAY :
LAST_DAY(date)
Example :
SELECT last_day(’20-nov-04’)
FROM dual;
Result:30-NOV-04
3. MONTHS_BETWEEN :
MONTHS_BETWEEN(date,date)
Example :
SELECT months_between(’20-feb-05’,’20-nov-04’)
FROM dual;
Result:3
4. NEXT_DAY :
NEXT_DAY (date,char);
Example :
SELECT next_day(’16-nov-04’,’sunday’)
FROM dual;
Result:21-NOV-04
5. ALTER SESSION
6.
7. :
Example :
Alter session
set nls_date_format = ’DD.MM.YYYY’;
1. TO_DATE :
TO_DATE(‘character’[,fmt])
Example :
SELECT TO_DATE(’3004SEP04’,’DD-MON-YYYY’)
FROM dual;
Result:30-SEP-04
2. TO_CHAR :
TO_CHAR(date,format)
Example :
Result: sep 3 04
3. TO_NUMBER :
format is optional. This is the format that will be used to convert string1 to
a number.
Example :
Result: 1210.73
TOPIC 6
1. UNION :
The UNION query allows to combine the result sets of 2 or more "select"
queries. It removes duplicate rows between the various "select"
statements.Each SQL statement within the UNION query must have the
same number of fields in the result sets with similar data types.
Syntax :
Example :
Select supplier_id
from suppliers
UNION
select supplier_id
from orders;
2. UNION ALL :
Example :
Select supplier_id
from suppliers
UNION ALL
select supplier_id
from orders;
Syntax
Example :
select supplier_id
from suppliers
INTERSECT
select supplier_id
from orders;
4. MINUS :
The MINUS query returns all rows in the first query that are not
returned in the second query.Each SQL statement within the MINUS query
must have the same number of fields in the result sets with similar data
types.
Syntax
Example :
Select supplier_id
from suppliers
MINUS
select supplier_id
from orders;
TOPIC 7
GRANT
REVOKE
1. GRANT :
The objects created by one user are accessible to another user only
when the owner gives permission by using Grant statement.
Syntax
Example :
GRANT select,update
ON stud
TO scott
2. REVOKE :
Syntax
REVOKE select,update
ON stud
FROM scott
COMMIT
ROLLBACK
1. COMMIT :
The COMMIT statement in SQL marks the final step in the processing
of a database transaction.It makes permanent any changes made during
that transaction.
COMMIT;
2. ROLLBACK :
ROLLBACK;
TOPIC 8
8.1 VIEWS :
Creation of Views :
Syntax
CREATE VIEW viewname AS
SELECT columnname, columnname
FROM tablename
WHERE columnname=expression list;
Example 1
CREATE VIEW vstud AS
SELECT name,no
FROM stud;
TOPIC 9
Example 1
Example 2
Example 3
To find all suppliers whose name is 5 characters long, where the first two
characters is 'Sm' and the last two characters is 'th'.
9.2 IN CONDITION :
SELECT columns
FROM tables
WHERE column1 in (value1, value2, .... value_n);
This SQL statement will return the records where column1 is value1,
value2..., or value_n. The IN function can be used in any valid SQL
statement - select, insert, update, or delete.
Example 1
SELECT *
FROM supplier
WHERE supplier_name in ( 'IBM' , 'Hewlett Packard', 'Microsoft');
This would return all rows where the supplier_name is either IBM, Hewlett
Packard, or Microsoft.
Example 2
SELECT *
FROM supplier
WHERE supplier_name not in ( 'IBM' , 'Hewlett Packard',
'Microsoft');
This would return all rows where the supplier_name is neither IBM,
Hewlett Packard, or Microsoft.
SELECT columns
FROM tables
WHERE column1 between value1 and value2;
This SQL statement will return the records where column1 is within the
range of value1 and value2 (inclusive).
Example 1
SELECT *
FROM suppliers
WHERE supplier_id between 5000 AND 5010;
This would return all rows where the supplier_id is between 1000 and
5010, inclusive.
Example 2
SELECT *
FROM orders
WHERE order_date between to_date ('2003/01/01', 'yyyy/mm/dd')
AND to_date ('2003/12/31', 'yyyy/mm/dd);
This SQL statement would return all orders where the order_date is
between Jan 1, 2003 and Dec 31, 2003 (inclusive).
Example 3
SELECT *
FROM suppliers
WHERE supplier_id not between 5000 and 5500;
SELECT columns
FROM tables
WHERE EXISTS ( subquery );
Example
SELECT *
FROM suppliers
WHERE EXISTS
(select *
from orders
where suppliers.supplier_id = orders.supplier_id);
This select statement will return all records from the suppliers table
where there is at least one record in the orders table with the same
supplier_id.
Example
SELECT *
FROM suppliers
WHERE NOT EXISTS
(select *
from orders
where suppliers.supplier_id = orders.supplier_id);
This will return all records from the suppliers table where there are no
records in the orders table for the given supplier_id.
Example
To return the name of the department and the total sales (in the associated
department).
Example
Syntax
Example
To return the name of the department and the total sales (in the
associated department). The HAVING clause will filter the results so that
only departments with sales greater than $1000 will be returned.
Example
The ORDER BY clause allows you to sort the records in your result set.
The ORDER BY clause can only be used in SELECT statements. The ORDER
BY clause sorts the result set based on the columns specified. If the ASC or
DESC value is omitted, the system assumed ascending order.
Syntax
SELECT columns
FROM tables
WHERE predicates
ORDER BY column ASC/DESC;
Example 1
SELECT supplier_city
FROM supplier
WHERE supplier_name = 'IBM'
ORDER BY supplier_city;
Example 2
SELECT supplier_city
FROM supplier
WHERE supplier_name = 'IBM'
ORDER BY supplier_city DESC;
This would return all records sorted by the supplier_city field in descending
order.