sql sequel -structured query language sql1 -1986 sql2 - 1992 better support for algebraic operations...

25
SQL SeQueL -Structured Query Language SQL1 -1986 SQL2 - 1992 better support for Algebraic operations SQL3 - 1999 Post-Relational row and column types, stored procedures, triggers, references, large objects

Upload: marilynn-rose

Post on 05-Jan-2016

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SQL SeQueL -Structured Query Language SQL1 -1986 SQL2 - 1992 better support for Algebraic operations SQL3 - 1999 Post-Relational row and column types,

SQL

SeQueL-Structured Query LanguageSQL1 -1986 SQL2 - 1992

better support for Algebraic operations

SQL3 - 1999 Post-Relational row and column types, stored procedures,

triggers, references, large objects

Page 2: SQL SeQueL -Structured Query Language SQL1 -1986 SQL2 - 1992 better support for Algebraic operations SQL3 - 1999 Post-Relational row and column types,

SQL v. Access Query Builder

‘Standard’ Database language used in all commercial DBMS’s

GUI query-builder in Access cannot build complex SQL

SQL is a textural language - store, edit, generate

There are jobs in it!

Page 3: SQL SeQueL -Structured Query Language SQL1 -1986 SQL2 - 1992 better support for Algebraic operations SQL3 - 1999 Post-Relational row and column types,

SQLDATA MANIPULATION (DML) - factbase

QUERYSELECT

UPDATEUPDATEDELETEINSERT

DATA DEFINITION (DDL) -schemaCREATE, ALTER, DROP

DATA CONTROL (DCL) - access controlGRANT,REVOKE

Page 4: SQL SeQueL -Structured Query Language SQL1 -1986 SQL2 - 1992 better support for Algebraic operations SQL3 - 1999 Post-Relational row and column types,

PROJECT - the columns

SELECT dname FROM dept;

Computed columnsSELECT ename,sal*12 FROM emp;

Renamed columnsSELECT ename, sal*12 as annualSalaryFROM emp;

Page 5: SQL SeQueL -Structured Query Language SQL1 -1986 SQL2 - 1992 better support for Algebraic operations SQL3 - 1999 Post-Relational row and column types,

RESTRICT - the rows

SELECT * FROM empWHERE job=‘Analyst’;

complex conditions:SELECT * FROM empWHERE job=‘Analyst’ or dept=20;

Page 6: SQL SeQueL -Structured Query Language SQL1 -1986 SQL2 - 1992 better support for Algebraic operations SQL3 - 1999 Post-Relational row and column types,

RESTRICT and PROJECT

in SQL:SELECT Mgr FROM Emp WHERE job=‘Analyst’ or deptno=10;

may produce duplicate rows:SELECT DISTINCT Mgr FROM Emp WHERE job=‘Analyst’ or deptno=10;

Page 7: SQL SeQueL -Structured Query Language SQL1 -1986 SQL2 - 1992 better support for Algebraic operations SQL3 - 1999 Post-Relational row and column types,

FUNCTIONS

STRINGSLIKE

DATE SYSDATE..

STATISTICAL FUNCTIONS COUNT, AVG, MIN, MAX, SUM

GENERATE AGGREGATE VALUESSELECT SUM(sal) FROM emp;

shows total salary over all emps

Page 8: SQL SeQueL -Structured Query Language SQL1 -1986 SQL2 - 1992 better support for Algebraic operations SQL3 - 1999 Post-Relational row and column types,

GROUP BY

OFTEN WANT AGGREGATE GROUPS OF ROWSSELECT deptno, sum(sal) FROM emp GROUP BY deptno;

Here the rows are first grouped together with their common values, then any aggregation is done on each subgroup.

Page 9: SQL SeQueL -Structured Query Language SQL1 -1986 SQL2 - 1992 better support for Algebraic operations SQL3 - 1999 Post-Relational row and column types,

HAVING

Restrict Groups shown:SELECT deptno, sum(sal) FROM emp GROUP BY deptnoHAVING SUM(sal) > 10000;

After the groups have been aggregated, these new rows can be further selected.

Page 10: SQL SeQueL -Structured Query Language SQL1 -1986 SQL2 - 1992 better support for Algebraic operations SQL3 - 1999 Post-Relational row and column types,

SUBQUERY

List all departments employing analystsselect distinct deptno from emp

where job = ‘analyst’

List the names of departments employing analystsSELECT Dname FROM Dept WHERE Deptno IN ( select distinct deptno from emp where job = ‘analyst’);

Page 11: SQL SeQueL -Structured Query Language SQL1 -1986 SQL2 - 1992 better support for Algebraic operations SQL3 - 1999 Post-Relational row and column types,

No Subquery?

MySQL does not support subqueries (until version 4.1)

How can we do the same query without subqueriesSelect distinct dname From dept natural join empWhere job=‘analyst’;

Page 12: SQL SeQueL -Structured Query Language SQL1 -1986 SQL2 - 1992 better support for Algebraic operations SQL3 - 1999 Post-Relational row and column types,

UNION

SELECT deptno FROM dept WHERE dname like ‘%ale%’

UNIONSELECT deptno FROM emp

WHERE sal > 3000;

Page 13: SQL SeQueL -Structured Query Language SQL1 -1986 SQL2 - 1992 better support for Algebraic operations SQL3 - 1999 Post-Relational row and column types,

PRODUCT

SELECT * from dept,emp;

GENERATES N * M ROWS SO GENERALLY TOO LARGE (4 * 14 = 56 rows on the emp data)but

BASIS OF ALL JOINS

Page 14: SQL SeQueL -Structured Query Language SQL1 -1986 SQL2 - 1992 better support for Algebraic operations SQL3 - 1999 Post-Relational row and column types,

PRODUCT THEN RESTRICT

SELECT * FROM Dept, emp WHERE dept.deptno = emp.deptno

called an EQUI-JOIN Now only matching rows in both tables

appearsince every emp has a non NULL deptno,

there will be as many rows in the join as there are rows in the emp table

Page 15: SQL SeQueL -Structured Query Language SQL1 -1986 SQL2 - 1992 better support for Algebraic operations SQL3 - 1999 Post-Relational row and column types,

Product, project, restrict

Can use SQL-2 operators in Access or MySQLSELECT dname, enameFROM dept inner join empon dept.deptno=emp.deptno;

Combine with restriction:SELECT dname, enameFROM dept inner join empon dept.deptno=emp.deptnowhere job=‘analyst’;

Page 16: SQL SeQueL -Structured Query Language SQL1 -1986 SQL2 - 1992 better support for Algebraic operations SQL3 - 1999 Post-Relational row and column types,

JOIN or SUBQUERY?

SELECT dnameFROM dept inner join empon dept.deptno=emp.deptnowhere job=‘analyst’

often several ways to express same query

Which is ‘better’?What is ‘better’?

Page 17: SQL SeQueL -Structured Query Language SQL1 -1986 SQL2 - 1992 better support for Algebraic operations SQL3 - 1999 Post-Relational row and column types,

Outer Join

Inner join returns only matching rowsWe can get non-matching rows too

A LEFT JOIN Bincludes all the non matching rows in A as

well

A RIGHT JOIN Bincludes all the non matching rows in B as

well

a FULL JOIN Bincludes non matching rows from both tables

Page 18: SQL SeQueL -Structured Query Language SQL1 -1986 SQL2 - 1992 better support for Algebraic operations SQL3 - 1999 Post-Relational row and column types,

M F23 fred 23 sue20 joe 30 alice34 bill 34 julie

Select * from M ,F23 fred 23 sue23 fred 30 alice23 fred 34 julie20 joe 23 sue20 joe 30 alice20 joe 34 julie34 bill 23 sue34 bill 30 alice34 bill 34 julie

Page 19: SQL SeQueL -Structured Query Language SQL1 -1986 SQL2 - 1992 better support for Algebraic operations SQL3 - 1999 Post-Relational row and column types,

M F23 fred 23 sue20 joe 30 alice34 bill 34 julie

Select * from M inner join F on M.age = F.age23 fred 23 sue23 fred 30 alice23 fred 34 julie20 joe 23 sue20 joe 30 alice20 joe 34 julie34 bill 23 sue34 bill 30 alice34 bill 34 julie

Page 20: SQL SeQueL -Structured Query Language SQL1 -1986 SQL2 - 1992 better support for Algebraic operations SQL3 - 1999 Post-Relational row and column types,

M F23 fred 23 sue20 joe 30 alice34 bill 34 julie

Select * from M left join on M.age = F.age23 fred 23 sue34 bill 34 julie

Page 21: SQL SeQueL -Structured Query Language SQL1 -1986 SQL2 - 1992 better support for Algebraic operations SQL3 - 1999 Post-Relational row and column types,

M F23 fred 23 sue20 joe 30 alice34 bill 34 julie

Select * from M right join on M.age = F.age23 fred 23 sue34 bill 34 julie

Page 22: SQL SeQueL -Structured Query Language SQL1 -1986 SQL2 - 1992 better support for Algebraic operations SQL3 - 1999 Post-Relational row and column types,

M F23 fred 23 sue20 joe 30 alice34 bill 34 julie

Select * from M full join on M.age = F.age23 fred 23 sue34 bill 34 julie

Page 23: SQL SeQueL -Structured Query Language SQL1 -1986 SQL2 - 1992 better support for Algebraic operations SQL3 - 1999 Post-Relational row and column types,

M F23 fred 23 sue20 joe 30 alice34 bill 34 julie

Select * from M, F on M.age > F.age23 fred 23 sue23 fred 30 alice23 fred 34 julie20 joe 23 sue20 joe 30 alice20 joe 34 julie34 bill 23 sue34 bill 30 alice34 bill 34 julie

Page 24: SQL SeQueL -Structured Query Language SQL1 -1986 SQL2 - 1992 better support for Algebraic operations SQL3 - 1999 Post-Relational row and column types,

Inner Join (natural Join)Select * from M inner join F on M.age=F.age;

Left (Outer) JoinSelect * from M left join F on M.age=F.age;

Right (Outer) JoinSelect * from M right join F on M.age=F.age;

Full (outer) JoinSelect * from M full join F on M.age=F.age;

Page 25: SQL SeQueL -Structured Query Language SQL1 -1986 SQL2 - 1992 better support for Algebraic operations SQL3 - 1999 Post-Relational row and column types,

Show all departments without employeesSelect dname from dept left join empon dept.deptno = emp.deptno

shows all the department names

where ename IS NULL;shows only the departments without

employees