Вы находитесь на странице: 1из 6

Introduction

page 1

SQL Data Query Language

page 2

Syntax rules of SQL commands


EMP
EMPNO ENAME
7839 7698 7782 7566 7654 7499 7844 7900 7521 7902 7369 7788 7876 7934 KING BLAKE CLARK JONES MARTIN ALLEN TURNER JAMES WARD FORD SMITH SCOTT ADAMS MILLER

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

1400 300 0 500

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;

Simple select command


SELECT attr1, attr2, ..., attrn FROM table_name;

DEPT
DEPTNO
10 20 30 40

DNAME
ACCOUNTING RESEARCH SALES OPERATIONS

LOC
NEW YORK DALLAS CHICAGO BOSTON

SELECT ename, job, sal FROM emp;

Arithmetical expressions
can contain: attributes names, numerical constants, and arithmetical operators: +, -, *, /

SALGRADE
GRADE LOSAL HISAL

1 2 3 4 5

700 1201 1401 2001 3001

1200 1400 2000 3000 9999

SELECT ename, sal*12 + comm FROM emp;

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')

SQL Data Query Language

page 3

SQL Data Query Language

page 4

Elimination of duplicate values


SELECT deptno FROM emp; SELECT DISTINCT deptno FROM emp;

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;

SELECT ename FROM emp WHERE job IN ('ANALYST', 'MANAGER');


3. LIKE Used in character string comparisons with pattern matching.

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];

SELECT job FROM emp WHERE job LIKE 'AN%';


4. IS NULL Tests whether the value on an attribute is null.

SELECT ename FROM emp WHERE mgr IS NULL;


5. 6. 7. 8. NOT BETWEEN ... AND ... NOT IN NOT LIKE IS NOT NULL

Mathematical operators
= != > >= < <=

SELECT ename FROM emp WHERE job='CLERK'; SELECT ename FROM emp WHERE sal>1000;

SQL Data Query Language

page 5

SQL Data Query Language

page 6

Complex WHERE clause


SELECT attr1, attr2, ..., attrn FROM table WHERE attri operator attrj|expression AND attrk operator attrl|expression OR attrm operator attrn|expression ORDER BY attr1[ASC|DESC], ..., attrn[ASC|DESC]; AND has higher priority than OR. This priority can be changed using parenthesis.

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 deptno, AVG(sal) FROM emp GROUP BY deptno;


Caution! All attributes in the select clause must appear in the group by clause.

SELECT ename FROM emp WHERE sal > 1000 AND job = 'CLERK'; SELECT ename FROM emp WHERE sal > 1500 AND job = 'SALESMAN' OR job = 'ANALYST';

SELECT deptno, job, AVG(sal) FROM emp GROUP BY deptno, job;


before creating groups some records can be eliminated by means of the where clause

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.

SELECT AVG(sal) FROM emp; SELECT COUNT(*) FROM emp

SQL Data Query Language SELECT sex, MAX(sal) FROM emp

page 7

SQL Data Query Language

page 8

HAVING MAX(sal) > 2000 GROUP BY sex;

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;

SQL Data Query Language

page 9

SQL Data Query Language

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.

SELECT ename, dname FROM emp, dept;

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;

SQL Data Query Language

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;

Subqueries in the HAVING clause

Вам также может понравиться