Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
functionality. Data Query Language (DQL) mainly deals with SQL SELECT statement for
retrieving data from a database
DQL SELECT
Here is the basic syntax for a SELECT query
SELECT [ col1,col2.... or * ] FROM table_name;
Now let us see a few examples to understand SQL. We will consider an employee table - emp
that contains all the employee data for an organization. Without knowing much about what
are the data in store in this table, let us start with some SQL examples. Let's try to display all
the columns and records in the Employee table:
SELECT * FROM emp;
WHERE Clause
The WHERE Clause is used to specify the criteria of selection.
Example: List all employees who belong to department number 40
SELECT empno, ename, deptno FROM emp WHERE deptno = 40;
DISTINCT Clause
The DISTINCT Clause is used to eliminate duplicate values from the dataset and to display
only the unique values.
Example: To list the unique jobs in the employee table
SELECT DISTINCT job FROM emp;
Operators
Arithmetic
+,-,*,/
Relational
=,<,>,<=,>=,<>,!=,^=
Logical or Boolean
AND,OR,NOT
Set Operators
Others
Arithmetic Operator
SELECT sal, sal * 0.5 AS Bonus FROM emp;
SELECT sysdate - 1 AS Yesterday, sysdate Today, sysdate + 1 Tomorrow FROM
DUAL;
Relational Operator
Example: List the employee name whose employee number is 7900
SELECT ename FROM emp WHERE empno = 7900;
Logical Operators
Example: List the employees who are getting salary in the range of 1500 and 3000
SELECT empno, ename, sal FROM emp WHERE sal >= 1500
AND
Example: List the employee number and names of department 10, department 20
SELECT empno, ename, sal FROM emp WHERE deptno = 10 OR deptno = 20;
Set Operators
Each of these operations combines the results of two select statements into a single result set.
**data types should be same of both the tables
The UNION operator returns the records retrieved by either of the queries
The INTERSECT operator returns those rows which are retrieved by both the queries
The MINUS operator returns all rows retrieved by the first query but not by the
second query
Note: The structure of emp_kol and emp_mum table must be exactly the same.
Example: List all employees in Kolkata and Mumbai location
SELECT * FROM emp_kol
UNION ALL
SELECT * FROM emp_mum;
Example: List employees having entry in both Kolkata and Mumbai location
SELECT * FROM emp_kol
INTERSECT
SELECT * FROM emp_mum;
Example: List employees having entry in Kolkata but not in Mumbai location
SELECT * FROM emp_kol
MINUS
SELECT * FROM emp_mum;
Other Operators
Examples:
SELECT
SELECT
SELECT
SELECT
empno,
empno,
empno,
empno,
ename, deptno FROM emp WHERE sal BETWEEN 1500 AND 2500;
ename, sal FROM emp WHERE deptno IN ( 10, 20 );
ename FROM emp WHERE ename LIKE S%;
ename,comm FROM emp WHERE comm IS NULL;
Examples:
SELECT
SELECT
SELECT
SELECT
SELECT
SELECT
Can be nested
Numeric
Character
Date
Conversion
Others
CEIL: CEIL(n): The ceil function returns the largest integer value that is greater than or
equal to a number
SELECT CEIL( 23.2 ) FROM dual;
o/p: 24
FLOOR: FLOOR(n): The floor function returns the smallest integer value that is greater than
or equal to a number
SELECT FLOOR( 56.99 ) FROM dual;
o/p: 56
SIGN: SIGN(n): The sign function returns a value indicating the sign of a number
SELECT SIGN( -10 ), SIGN( 0 ), SIGN( 10 ) FROM dual;
o/p -1 0 1
FROM emp;
INSTR: Search characters in a string literal- INSTR( STRING, search strng, start position )
SELECT INSTR( ename, 'S', 1 ) FROM emp;
SUBSTR: Returns a portion of the string literal- SUBSTR( STRING, start pos, no. of
characters )
SELECT SUBSTR( ename, 1, 3 ) FROM emp;
SOUNDEX: SOUNDEX(string)
SELECT ename FROM emp WHERE SOUNDEX( ename ) = SOUNDEX( 'SMEETH' );
LAST_DAY: Finds the last date of the month for the input date
SELECT LAST_DAY( hiredate ) FROM emp;
Conversion Functions
TO_CHAR(X): Converts the value of X to a character or converts a date to a character
SELECT TO_CHAR( 1981 ) FROM dual;
SELECT TO_CHAR( hiredate, 'YYYYMMDD' ) FROM dual;
Aggregate Functions
SUM: Returns the sum of the column values provided as parameters to the function
SELECT SUM( sal ) "Total Sal" FROM emp WHERE deptno = 20;
AVG: Returns average value of the column values provided as parameters to the function
SELECT AVG( Sal ) "Average Sal" FROM emp;
-count( 1 ): faster;
-count( comm )- will omit records with NULL.
-count( * ): slow from performance standpoint; can use COUNT( 1 ) instead.
SELECT COUNT( empno ) "Total Employees" FROM emp;
Miscellaneous Functions
decode: Facilitates conditional inquiries by doing the work of a CASE or IF-THEN-ELSE
statement. Syntax:
decode compares to the search and if exp_x matches, returns result_x. If not, returns default,
or, if default is left out, null.
SELECT empno, ename,
DECODE( deptno, 10, 'Ten', 20, 'Twenty', 'Other' ) as department
FROM emp;
SELECT empno, ename,
DECODE( sign( sal - 2000 ), 1, 'Above target',
-1, 'Below target', 'On Target' )
FROM emp;
Case: A CASE expression selects a result and returns it. To select the result, the CASE
expression uses a selector, an expression whose value is used to select one of several
alternatives. Allows a more compact notation and some elimination of repetition with respect
to what otherwise would be expressed with an IF construct. Syntax:
case n
when 1 then Action1;
when 2 then Action2;
when 3 then Action3;
else ActionOther;
end case;
SELECT
COUNT( CASE WHEN sal < 2000 THEN 1 ELSE NULL END ) count1,
COUNT( CASE WHEN sal BETWEEN 2001 AND 4000 THEN 1 ELSE NULL END ) count2,
COUNT( CASE WHEN sal > 4000 THEN 1 ELSE NULL END ) count3
FROM emp;
Note: NULL values in the output of rollup operations typically mean that the row contains
subtotal or grand total information. Output of the ROLLUP function:
DEPTNO
JOB
SALARY
----------------------------10
CLERK
10
MANAGER
10
PRESIDENT
10
20
CLERK
20
ANALYST
20
MANAGER
20
30
CLERK
1300
2450
5000
8750
1900
6000
2975
10875
950
30
30
30
MANAGER
SALESMAN
2850
5600
9400
29025
CUBE: The cube operation calculates all levels of subtotals on horizontal lines across
spreadsheets of output and create cross-tab summaries on multiple vertical columns in those
spreadsheets. e.g.
SELECT deptno,job,SUM( sal ) AS salary
FROM emp
GROUP BY CUBE( deptno, job );
10
10
10
10
20
20
20
20
30
30
30
30
CLERK
MANAGER
PRESIDENT
CLERK
ANALYST
MANAGER
CLERK
MANAGER
SALESMAN
29025
4150
6000
8275
5600
5000
8750
1300
2450
5000
10875
1900
6000
2975
9400
950
2850
5600
Table Joins
If we require data from more than one table in the database, a join is used.
Types of Join
EQUI
NON-EQUI
OUTER
SELF
EQUI JOIN
The columns compared by = operator are called join columns and the join operation is
called an EQUI JOIN
SELF JOIN
It is possible using table labels (aliases) to join a table to itself, as if they were two
separate tables
Example: List the employees working in the same department as that of employee 7900
SELECT b.ename
FROM emp a, emp b
WHERE a.deptno = b.deptno
AND a.empno = 7900;
OUTER JOIN
If a row does not satisfy a join condition, then the row will not appear in the query
result
The missing row(s) can be returned if an outer join operator is used in the join
condition
The operator is plus sign enclosed in parentheses (+), and is placed on the side of the
join(table) which is deficient in information
Sub-queries
A sub-query is a form of an SQL statement that appears inside another SQL statement
Types of Sub-queries
Single-row Sub-queries
SELECT ename FROM emp
WHERE deptno = (
SELECT deptno FROM dept
WHERE dname = 'ACCOUNTING' );
Multi-row Sub-queries
SELECT ename FROM emp
WHERE deptno IN (
SELECT deptno FROM dept );
ALL: the condition evaluates to true, if there exist all the rows selected by the sub-query for
which the comparison holds.
SELECT * FROM emp WHERE sal >= ALL
(SELECT sal FROM emp WHERE deptno = 30)
and deptno <> 30;
Inline Views: The inline view is a construct in Oracle SQL where you can place a query in
the SQL FROM clause, just as if the query was a table name e.g.
SELECT ename, sal FROM (
select * from emp ORDER BY sal desc )
WHERE ROWNUM < 6;
Multiple-column Sub-queries: When you want to compare more than one columns in a subquery. e.g.
SELECT ename, job FROM emp
WHERE ( deptno, sal ) IN (
SELECT deptno, max( sal )
FROM emp GROUP BY deptno );
Co-related Sub-queries
A co-related query is used to answer questions whose answers depends on the values
in each row of the parent query
EXISTS: The EXISTS condition is considered "to be met" if the sub-query returns at least
one row. e.g.
SELECT deptno, dname
FROM dept a WHERE EXISTS (
SELECT empno FROM emp e
WHERE a.deptno = e.deptno );
SELECT deptno, dname
FROM dept a WHERE NOT EXISTS (
SELECT empno FROM emp e
WHERE a.deptno = e.deptno );
SELECT ename
FROM emp a
WHERE EXISTS (
SELECT * FROM emp b
WHERE b.mgr = a.empno );
With clause lets us factor out the sub-query, give it a name, then reference that name
multiple times within the original complex query
This improves the performance of this query by having Oracle 9i execute the query
only once, then simply reference it at the appropriate points in the main query. e.g.
WITH summary AS
( SELECT dname, SUM( sal ) AS dept_total
FROM emp,dept
WHERE emp.deptno = dept.deptno
GROUP BY dname )
SELECT dname,dept_total
FROM summary
WHERE dept_total > (
SELECT SUM( dept_total ) * 1 / 3
FROM summary )
ORDER BY dept_total desc;