Syntax Rules of SQL Commands: - in One or More Lines - With Small or Capital Letters
Syntax Rules of SQL Commands: - in One or More Lines - With Small or Capital Letters
page 1
page 2
JOB
PRESIDENT MANAGER MANAGER MANAGER SALESMAN SALESMAN SALESMAN CLERK SALESMAN ANALYST CLERK ANALYST CLERK CLERK
MGR
7839 7839 7839 7698 7698 7698 7698 7698 7566 7902 7566 7788 7782
HIREDATE
17-NOV-81 01-MAY-81 09-JUN-81 02-APR-81 28-SEP-81 20-FEB-81 08-SEP-81 03-DEC-81 22-FEB-81 03-DEC-81 17-DEC-80 09-DEC-82 12-JAN-83 23-JAN-82
SAL
COMM
DEPTNO
10 30 10 20 30 30 30 30 30 20 20 20 20 10
5000 2850 2450 2975 1250 1600 1500 950 1250 3000 800 3000 1100 1300
1. Each command has so called clauses that start with a keyword. 2. A command can be written: in one or more lines with small or capital letters 3. Each command has to be ended by semicolon in order to be run
select * from dept; SELECT * FROM dept; select * from dept;
DEPT
DEPTNO
10 20 30 40
DNAME
ACCOUNTING RESEARCH SALES OPERATIONS
LOC
NEW YORK DALLAS CHICAGO BOSTON
Arithmetical expressions
can contain: attributes names, numerical constants, and arithmetical operators: +, -, *, /
SALGRADE
GRADE LOSAL HISAL
1 2 3 4 5
Null values
SELECT ename, sal, comm FROM emp; SELECT ename, sal, NVL(comm, 0)FROM emp;
NVL (date_column, '01-JAN-88') NVL(number_column, 9) NVL(char_column, 'STRING')
page 3
page 4
SQL operators
1. BETWEEN ... AND ... Tests whether the value of an attribute is within the specified range of values.
ORDER BY clause
allows to sort the results of a query has to be placed at the end of a SQL command SELECT attr1, attr2, ..., attrn FROM table ORDER BY attr1[ASC | DESC], ..., attrn[ASC | DESC];
SELECT ename FROM emp WHERE sal BETWEEN 3000 AND 5000;
2. IN Tests whether the value of an attribute is equal to any value from the specified set. The set can contain either numerical or character or date values.
SELECT ename, job, sal FROM emp ORDER BY deptno DESC, ename;
WHERE clause
allows to select records that fulfil the specified conditions SELECT attr1, attr2, ..., attrn FROM table WHERE attri operator attrj|expression ORDER BY attr1[ASC | DESC], ..., attrn[ASC | DESC];
Mathematical operators
= != > >= < <=
SELECT ename FROM emp WHERE job='CLERK'; SELECT ename FROM emp WHERE sal>1000;
page 5
page 6
WHERE deptno=20;
GROUP BY clause
divides group into subgroups SELECT attr1, attr2, ..., attrn, group_function FROM table WHERE ... GROUP BY attr1, attr2, ..., attrn;
SELECT ename FROM emp WHERE sal > 1000 AND job = 'CLERK'; SELECT ename FROM emp WHERE sal > 1500 AND job = 'SALESMAN' OR job = 'ANALYST';
Group functions
groups the selected rows based on the value of the expression of each row and returns a single row of summary information for each group functions AVG ( [distinct|all] expression ) COUNT ( [distinct|all] expression ) MAX ( [distinct|all] expression ) MIN ( [distinct|all] expression ) SUM ( [distinct|all] expression )
SELECT job, AVG(sal) FROM emp WHERE job != 'MANAGER' GROUP BY job;
HAVING clause
Restricts the groups of rows returned to those groups for which the specified condition is TRUE. If you omit this clause, Oracle returns summary rows for all groups.
page 7
page 8
SELECT dept.deptno, dname, ename FROM emp, dept WHERE emp.deptno = dept.deptno;
Using table aliases
Joins
A join is a query that combines rows from two or more tables. Oracle performs a join whenever multiple tables appear in the query's FROM clause. The query's select list can select any attribute from any of these tables. If any two of these tables have an attribute name in common, you must qualify all references to these columns throughout the query with table names to avoid ambiguity. In addition to join conditions, the WHERE clause of a join query can also contain other conditions that refer to attributes of only one table. These conditions can further restrict the rows returned by the join query.
SELECT D.deptno, dname, ename FROM emp E, dept D WHERE E.deptno = D.deptno;
Self join
A self join is a join of a table to itself. This table appears twice in the from clause and is followed by table aliases.
Equijoin
An equijoin is a join with a join condition containing an equality operator. An equijoin combines rows that have equivalent values for the specified columns. select attr1, attr2, ..., attrn from tableA, tableB where tableA.attrm = tableB.attrn;
page 9
page 10
SELECT W.ename worker, B.ename boss FROM emp W, emp B WHERE W.mgr = B.empno ORDER BY worker;
Subqueries
A subquery is a form of the SELECT command that appears inside another SQL statement. The statement containing a subquery is called the parent statement. The rows returned by the subquery are used by the parent statement. A subquery returning ONE record/value select attr1, attr2, ..., attrn from tableA where attrm = ( select attr1 from tableB where attri = ( select ... ) ); To determine who works in Taylor's department, issue the following statement:
Cartesian Product
If two tables in a join query have no join condition, Oracle returns their Cartesian product. Oracle combines each row of one table with each row of the other.
Outer Join
An outer join returns all rows that satisfy the join condition and those rows from one table for which no rows from the other satisfy the join condition. To write a query that performs an outer join of tables A and B and returns all rows from A, apply the outer join operator (+) to all columns of B in the join condition. For all rows in A that have no matching rows in B, Oracle returns NULL. This query uses an outer join to return departments and their managers and additionally all these employees that are not managers
SELECT ename, deptno FROM emp WHERE deptno = (SELECT deptno FROM emp WHERE ename = 'TAYLOR');
A subquery returning MANY records/values select attr1, attr2, ..., attrn from tableA where attrm in (select attr1 from tableB where attri in ( select ... ) );
SELECT dept.deptno, dname, ename FROM emp, dept WHERE emp.deptno (+)= dept.deptno;
page 11
SQL Data Query Language SELECT attr1, attr2, ..., attrn, group_function FROM tableA A WHERE ... GROUP BY attr1, attr2, ..., attrn HAVING group_function operator ( SELECT... );
page 12
SELECT ename, sal, deptno FROM emp WHERE sal IN (SELECT MIN(sal) FROM emp GROUP BY deptno);
Correlated Subqueries
A correlated subquery is a subquery that contains in its WHERE clause a reference to a row from the parent query. A correlated subquery is evaluated once per row processed by the parent statement. SELECT attr1, attr2, ..., attrn FROM tableA A WHERE attrm operator ( SELECT attri FROM tableB B WHERE B.attro operator A.attrp );
The following statement returns the number of this department that pays monthly the highest total salary to its employees.
SELECT deptno, SUM(sal) FROM emp GROUP BY deptno HAVING SUM(sal)= (SELECT MAX(SUM(sal)) FROM emp GROUP BY deptno);
The following statement returns data about employees whose salaries exceed the averages for their departments.
SELECT deptno, ename, sal FROM emp E WHERE sal > (SELECT AVG(sal) FROM emp WHERE deptno = E.deptno) ORDER BY deptno;