sql sequel -structured query language sql1 -1986 sql2 - 1992 better support for algebraic operations...
TRANSCRIPT
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
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!
SQLDATA MANIPULATION (DML) - factbase
QUERYSELECT
UPDATEUPDATEDELETEINSERT
DATA DEFINITION (DDL) -schemaCREATE, ALTER, DROP
DATA CONTROL (DCL) - access controlGRANT,REVOKE
PROJECT - the columns
SELECT dname FROM dept;
Computed columnsSELECT ename,sal*12 FROM emp;
Renamed columnsSELECT ename, sal*12 as annualSalaryFROM emp;
RESTRICT - the rows
SELECT * FROM empWHERE job=‘Analyst’;
complex conditions:SELECT * FROM empWHERE job=‘Analyst’ or dept=20;
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;
FUNCTIONS
STRINGSLIKE
DATE SYSDATE..
STATISTICAL FUNCTIONS COUNT, AVG, MIN, MAX, SUM
GENERATE AGGREGATE VALUESSELECT SUM(sal) FROM emp;
shows total salary over all emps
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.
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.
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’);
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’;
UNION
SELECT deptno FROM dept WHERE dname like ‘%ale%’
UNIONSELECT deptno FROM emp
WHERE sal > 3000;
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
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
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’;
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’?
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
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
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
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
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
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
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
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;
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