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

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Advertisements

BACS 485—Database Management Advanced SQL Overview Advanced DDL, DML, and DCL Commands.
TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
SQL Subqueries Objectives of the Lecture : To consider the general nature of subqueries. To consider simple versus correlated subqueries. To consider the.
Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved.
Set operators (UNION, UNION ALL, MINUS, INTERSECT) [SQL]
Structure Query Language (SQL) COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
Introduction to Structured Query Language (SQL). Origins & history Early 1970’s – IBM develops Sequel as part of the System R project at its San Hose.
SQL zSeQueL zSQL zSQL ybetter support for Algebraic operations zSQL Post-Relational yrow and column types, stored procedures, triggers,
ISD3 Lecture 4 - Databases, SQL and MySQL. dept deptno dname location emp empno ename not null job not null hiredate sal comm manager The EMP DEPT database.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Database Systems More SQL Database Design -- More SQL1.
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
HAP 709 – Healthcare Databases SQL Data Manipulation Language (DML) Updated Fall, 2009.
4-1 Copyright  Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Oracle Database Administration Lecture 2 SQL language.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
Relational Algebra - Chapter (7th ed )
Lecture 8 Database Theory & Practice (2) : The Relational Data Model UFCEKG-20-2 Data, Schemas & Applications.
Using Special Operators (LIKE and IN)
ACTION QUERIES (SQL COMMANDS ) STRUCTURED QUERY LANGUAGE.
1 Information Retrieval and Use (IRU) CE An Introduction To SQL Part 1.
Joins & Sub-queries. Oracle recognizes that you may want data that resides in multiple tables drawn together in some meaningful way. One of the most important.
SELECT Statements Lecture Notes Sree Nilakanta Fall 2010 (rev)
An Introduction To SQL - Part 1 (Special thanks to Geoff Leese)
10 Creating and Managing Tables Objectives At the end of this lesson, you will be able to: Describe the main database objects Create tables Describe.
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
SQL. Relating Multiple Tables Relational Database Terminology Row PK Column FK Field NULL.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
More SQL Data Management Topics zIntegrity Constraints zOuter Join zUnion.
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Displaying Data from Multiple Tables (SQL99 Syntax with examples)
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Subqueries These slides are licensed under.
1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.
An Introduction To SQL Part 2 (Special thanks to Geoff Leese)
1 Information Retrieval and Use (IRU) An Introduction To SQL Part 2.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
(SQL - Structured Query Language)
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Collection Operators These slides are.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Collection Operators These slides are.
Lecture 2 Joins and sub-queries. 2 Topics zJoins ySimple; Outer zSub-queries yaliases zIN Operator zNULL values zSaving and running files.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
More SQL: Complex Queries,
Lecture 3 : Structured Query Language (SQL)
Aggregating Data Using Group Functions
PL/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-1
Database Systems: Design, Implementation, and Management Tenth Edition
Introduction to Database Systems, CS420
Aggregating Data Using Group Functions
Interacting with the Oracle Server
Writing Correlated Subqueries
(SQL) Aggregating Data Using Group Functions
5-2-7 : تقسيم الصفوف في الجدول إلي مجموعات :-
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Database systems Lecture 3 – SQL + CRUD
SQL Fundamentals in Three Hours
Contents Preface I Introduction Lesson Objectives I-2
Lecture 16 : The Relational Data Model
Database Systems: Design, Implementation, and Management Tenth Edition
Lecture 16 : The Relational Data Model
Database Programming Using Oracle 11g
Presentation transcript:

SQL SeQueL -Structured Query Language SQL SQL better support for Algebraic operations SQL 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!

SQL DATA MANIPULATION (DML) - factbase QUERY SELECT UPDATE DELETE INSERT DATA DEFINITION (DDL) -schema CREATE, ALTER, DROP DATA CONTROL (DCL) - access control GRANT,REVOKE

PROJECT - the columns SELECT dname FROM dept; Computed columns SELECT ename,sal*12 FROM emp; Renamed columns SELECT ename, sal*12 as annualSalary FROM emp;

RESTRICT - the rows SELECT * FROM emp WHERE job=‘Analyst’; complex conditions: SELECT * FROM emp WHERE 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 STRINGS LIKE DATE SYSDATE.. STATISTICAL FUNCTIONS COUNT, AVG, MIN, MAX, SUM GENERATE AGGREGATE VALUES SELECT SUM(sal) FROM emp; shows total salary over all emps

GROUP BY OFTEN WANT AGGREGATE GROUPS OF ROWS SELECT 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 deptno HAVING SUM(sal) > 10000; After the groups have been aggregated, these new rows can be further selected.

SUBQUERY List all departments employing analysts select distinct deptno from emp where job = ‘analyst’ List the names of departments employing analysts SELECT 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 subqueries Select distinct dname From dept natural join emp Where job=‘analyst’;

UNION SELECT deptno FROM dept WHERE dname like ‘%ale%’ UNION SELECT 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 appear since 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 MySQL SELECT dname, ename FROM dept inner join emp on dept.deptno=emp.deptno; Combine with restriction: SELECT dname, ename FROM dept inner join emp on dept.deptno=emp.deptno where job=‘analyst’;

JOIN or SUBQUERY? SELECT dname FROM dept inner join emp on dept.deptno=emp.deptno where job=‘analyst’ often several ways to express same query Which is ‘better’? What is ‘better’?

Outer Join Inner join returns only matching rows We can get non-matching rows too A LEFT JOIN B includes all the non matching rows in A as well A RIGHT JOIN B includes all the non matching rows in B as well a FULL JOIN B includes non matching rows from both tables

MF 23 fred23 sue 20 joe30 alice 34 bill34 julie Select * from M,F 23 fred23 sue 23 fred30 alice 23 fred34 julie 20 joe23 sue 20 joe30 alice 20 joe34 julie 34 bill23 sue 34 bill30 alice 34 bill 34 julie

MF 23 fred23 sue 20 joe30 alice 34 bill34 julie Select * from M inner join F on M.age = F.age 23 fred23 sue 23 fred30 alice 23 fred34 julie 20 joe23 sue 20 joe30 alice 20 joe34 julie 34 bill23 sue 34 bill30 alice 34 bill 34 julie

MF 23 fred23 sue 20 joe30 alice 34 bill34 julie Select * from M left join on M.age = F.age 23 fred23 sue 34 bill 34 julie

MF 23 fred23 sue 20 joe30 alice 34 bill34 julie Select * from M right join on M.age = F.age 23 fred23 sue 34 bill 34 julie

MF 23 fred23 sue 20 joe30 alice 34 bill34 julie Select * from M full join on M.age = F.age 23 fred23 sue 34 bill 34 julie

MF 23 fred23 sue 20 joe30 alice 34 bill34 julie Select * from M, F on M.age > F.age 23 fred23 sue 23 fred30 alice 23 fred34 julie 20 joe23 sue 20 joe30 alice 20 joe34 julie 34 bill23 sue 34 bill30 alice 34 bill 34 julie

Inner Join (natural Join) Select * from M inner join F on M.age=F.age; Left (Outer) Join Select * from M left join F on M.age=F.age; Right (Outer) Join Select * from M right join F on M.age=F.age; Full (outer) Join Select * from M full join F on M.age=F.age;

Show all departments without employees Select dname from dept left join emp on dept.deptno = emp.deptno shows all the department names where ename IS NULL; shows only the departments without employees