SQL: Structured Query Language

Slides:



Advertisements
Similar presentations
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Advertisements

CS4432: Database Systems II Query Operator & Algebraic Expressions 1.
E-R Diagram for a Banking Enterprise
Oct 28, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
SQL Sangeeta Devadiga CS157A, Fall Outline Background Data Definition Basic Structure Set Operation.
Dec 4, 2003Murali Mani SQL B term 2004: lecture 14.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions.
Nov 24, 2003Murali Mani SQL B term 2004: lecture 12.
CS3431 SQL : Query Language. CS3431 SELECT-FROM-WHERE SELECT * FROM Student WHERE sName=“Greg” AND address=“320 FL”  (sName=“Greg” AND address=“320 FL”)
Murali Mani SQL. Murali Mani SELECT-FROM-WHERE SELECT * FROM Student WHERE sName=“Greg” AND address=“320 FL”  (sName=“Greg” AND address=“320 FL”) (Student)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
Instructor: Mohamed Eltabakh
CIS552SQL1 Data Definition Language Insertions Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations.
SQL I. SQL – Introduction  Standard DML/DDL for relational DB’s  DML = “Data Manipulation Language” (queries, updates)  DDL = “Data Definition Language”
Murali Mani Relational Algebra. Murali Mani What is Relational Algebra? Defines operations (data retrieval) for relational model SQL’s DML (Data Manipulation.
Chapter 7: SQL, the Structured Query Language Soid Quintero & Ervi Bongso CS157B.
Chapter 3: SQL Data Definition Language Data Definition Language Basic Structure of SQL Basic Structure of SQL Set Operations Set Operations Aggregate.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
SQL. Basic Structure SQL is based on set and relational operations with certain modifications and enhancements A typical SQL query has the form: select.
Lecture 6 Structured Query Language SQL Lecture 6 Structured Query Language SQL Instructor: Haya Sammaneh.
3.1 Chapter 3: SQL Schema used in examples p (omit 3.8.2, , 3.11)
Relational Algebra Instructor: Mohamed Eltabakh 1.
1 The Relational Model Instructor: Mohamed Eltabakh
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
Instructor: Jinze Liu Fall Basic Components (2) Relational Database Web-Interface Done before mid-term Must-Have Components (2) Security: access.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
CS3431-B111 The Relational Model Instructor: Mohamed Eltabakh
Advanced SQL: Triggers & Assertions
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
SQL: Structured Query Language Instructor: Mohamed Eltabakh 1 Part II.
Relational Algebra Instructor: Mohamed Eltabakh 1.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
SQL: Structured Query Language Instructor: Mohamed Eltabakh 1.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
SQL, the Structured Query Language
Chapter 3 Introduction to SQL
Database Systems SQL cont. Relational algebra
Introduction to SQL.
SQL : Query Language Part II CS3431.
CS 405G: Introduction to Database Systems
SQL Views CS542.
Instructor: Mohamed Eltabakh
The Relational Model Relational Data Model
CS 405G: Introduction to Database Systems
SQL : Query Language CS3431.
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
SQL Views and Updates cs3431.
Advanced SQL: Views & Triggers
Instructor: Mohamed Eltabakh
SQL: The Query Language Part 1
Instructor: Mohamed Eltabakh
Instructor: Mohamed Eltabakh
SQL: Structured Query Language
CS4222 Principles of Database System
CMPT 354: Database System I
Contents Preface I Introduction Lesson Objectives I-2
CSC 453 Database Systems Lecture
SQL: Structured Query Language
SQL: Structured Query Language
Instructor: Mohamed Eltabakh
SQL: Structured Query Language
Recap – Relational languages
Presentation transcript:

SQL: Structured Query Language Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu

SQL Language Data Definition Language (DDL) Create tables, specifying the columns and their types of columns, the primary keys, etc. Drop tables, add/drop columns, add/drop constraints – primary key, unique, etc. Data Manipulation Language (DML) Update, Insert, Delete tuples Query the data These are already covered Our focus today

Reminder About DDL Create “Students” relation Create “Courses” relation CREATE TABLE Students (sid: CHAR(20) Primary Key, name: CHAR(20) Not NULL, login: CHAR(10), age: INTEGER, gpa: REAL); CREATE TABLE Courses (cid: Varchar(20) Primary Key, name: string, maxCredits : integer, graduateFlag: boolean); Create “Enrolled” relation CREATE TABLE Enrolled (sid: CHAR(20) Foreign Key References (Students.sid), cid: Varchar(20), enrollDate: date, grade: CHAR(2), Constraints fk_cid Foreign Key cid References (Courses.cid)); Alter Table Enrolled Add Constraints fk_cid Foreign Key cid References Courses(cid));

Reminder About: Insert, Update, Delete This is performed using Data Manipulation Language of SQL (DML) Insertion Insert into Students values (“1111”, …); Deletion Delete from Students; Delete from Students Where sid = “1111”; Update Update Students Set GPA = GPA + 0.4; Update Students Set GPA = GPA + 0.4 Where sid = “1111”;

SQL Query Language SELECT Statement

SELECT-FROM-WHERE π SELECT <list of columns> σ relation name SELECT <list of columns> FROM <relation name> WHERE <conditions>;

SELECT-FROM-WHERE SELECT * FROM Student * Means “project all attributes” SELECT * FROM Student WHERE sName=“Greg” AND address=“320FL”; Student sNumber sName address professor 1 Dave 311FL MM 2 Greg 320FL 3 Matt ER sNumber sName address professor 2 Greg 320FL MM  (sName=“Greg” AND address=“320FL”) (Student)

SELECT-FROM-WHERE SELECT sNumber FROM Student WHERE sName=“Greg” AND address=“320FL”; Student sNumber sName address professor 1 Dave 311FL MM 2 Greg 320FL 3 Matt ER sNumber sName address professor 2 Greg 320FL MM πsNumber((sName=“Greg” AND address=“320FL”) (Student))

Select-From Query Only SELECT and FROM clauses are mandatory The WHERE clause is optional If not exist, then all records will be returned (there are no selection predicates) SELECT <list of columns> FROM <relation name>;

Select-From Query SELECT sNumber, sName FROM Student; address professor 1 Dave 320FL MM 2 Greg 3 Matt ER sNumber sName 1 Dave 2 Greg 3 Matt  (sNumber, sName) (Student)

Extended Projection SELECT <list of columns or expressions> The select clause can have expressions and constants SELECT <list of columns or expressions> FROM <relation name> WHERE <conditions>; Can also rename the fields or expressions using “AS”

Extended Projection SELECT ‘Name:’ || sName AS info, 0 AS gpa FROM Student WHERE address=“320FL”; Student sNumber sName address professor 1 Dave 320FL MM 2 Greg 3 Matt ER info gpa Name:Dave Name:Greg Name:Matt  (info  ‘Name:’||sName, gpa  0 ) ( (address=“320FL”) (Student))

Mapping between SQL and Relational Algebra  L ( C (R)) SELECT L FROM R WHERE C

Renaming Relations and Tuple Variables SELECT S1.sNumber AS num FROM Student S1 WHERE S1.sNumber >= 1; Tuple variable Student sNumber sName address professor 1 Dave 320FL MM 2 Greg 3 Matt ER num 1 2 3  (num  S1.sNumber) ( (S1.sNumber >= 1) (S1(Student)))

Where Clause The comparison operator depends on the data type For Numbers: <, >, <=, >=, =, <> What about Strings?? SELECT S1.sNumber AS num FROM Student S1 WHERE S1.sNumber >= 1;

String Operators Comparison Operators based on lexicographic ordering: =, <, >, <>, >=, <= Concatenation operator: || Pattern match: s LIKE p p denotes a pattern Can use wild characters in p such as _, % _ matches exactly any single character % matches zero or more characters SELECT ‘Name:’ || sName FROM Student WHERE address=“320FL”;

String Matching Example SELECT s1.sNumber AS num FROM Student S1 WHERE s1.sName LIKE ‘Da%’ Or S1.professor LIKE ‘M_’ ; sNumber sName address professor 1 Dave 320FL MM 2 Greg 3 Matt ER sNumber 1

Set Operators in SQL Set Semantics Bag Semantics Union, Intersect, Except Bag Semantics Union All, Intersect All, Except All The two relations R and S must have the same column names and types (Union Compatible)

Set Operations in SQL: Example Operators : UNION, INTERSECT, and EXCEPT (SELECT sName FROM Student) EXCEPT (SELECT sName FROM Student WHERE address=‘320FL’) SELECT sName FROM Student WHERE address <> ‘320FL’;

Set Operations in SQL: Example

Cartesian Product in SQL In Relation Algebra: R x S In SQL, add R and S to FROM clause No WHERE condition that links R and S SELECT * FROM Student, Professor; SELECT sName, pNumber FROM Student, Professor;

Cross Product - Example Student Professor sNumber sName address professor 1 Dave 320FL 2 Greg 3 Matt pNumber pName address 1 MM 141FL 2 ER 201FL SELECT * FROM Student, Professor; sNumber sName address professor pNumber pName 1 Dave 320FL MM 141FL 2 ER 201FL Greg 3 Matt

Theta Join in SQL In Relation Algebra: R ⋈C S In SQL, add R and S to FROM clause WHERE condition that links R and S with the join condition C SELECT * FROM Student, Professor WHERE Student.pNum = Professor.Number; Join condition

Theta Join Example Student Professor SELECT sNumber, sName, pName address profNum 1 Dave 320FL 2 Greg 3 Matt pNumber pName address 1 MM 141FL 2 ER 201FL SELECT sNumber, sName, pName FROM Student, Professor WHERE profNum = pNumber; sNumber sName pName 1 Dave MM 2 Greg 3 Matt ER  sNumber,sName,pName(Student ⋈(profNum=pNumber) Professor)

Natural Join Student ⋈ Professor Reminder: Join columns must have same names in both relations (R ⋈ S) Student ⋈ Professor SELECT * FROM Student NATURAL JOIN Professor; SELECT * FROM Student , Professor WHERE Student.pnumber = Professor.pnumber ; Explicitly add the equality join condition

Natural Join - Example Professor Student Student ⋈ Professor SELECT * sNumber sName address pNumber 1 Dave 320FL 2 Greg 3 Matt pNumber pName address 1 MM 141FL 2 ER 201FL SELECT * FROM Student , Professor WHERE Student.pNumber = Professor.pNumber ; sNumber sName address pNumber pName 1 Dave 320FL MM 141FL 2 Greg 3 Matt ER 201FL Student ⋈ Professor

Example Queries SELECT * FROM loan WHERE amount > 1200 ; SELECT loan_number FROM loan WHERE amount > 1200 ;

Example Queries SELECT customer_name FROM depositor Union FROM borrower;

Example Queries DBMS is smart enough !!! (Select first, then joins) SELECT customer_name FROM borrower B, loan L WHERE B.loan_number = L.loan_number AND L.branch_name = “Perryridge”;

Sorting: ORDER BY clause New optional clause that you can add to the SELECT statement called “ORDER BY” Allows sorting the returned records according to one or more fields SELECT * FROM Student WHERE sNumber >= 1 ORDER BY pNumber, sName; Default is ascending order SELECT * FROM Student WHERE sNumber >= 1 ORDER BY pNumber ASC, sName DESC;

Sorting: ORDER BY clause Student sNumber sName address pNumber 1 Dave 320FL 2 Greg 3 Matt SELECT * FROM Student WHERE sNumber >= 1 ORDER BY pNumber, sName DESC; sNumber sName address pNumber 2 Greg 320FL 1 Dave 3 Matt  (pNumber, sName DESC) ( (sNumber >= 1) (Student))

Duplicate Elimination in SQL New optional keyword “DISTINCT” Added in the SELECT clause SELECT DISTINCT … FROM … … Eliminate any duplicates from the answer

Duplicate Elimination: Example Student sNumber sName address professor 1 Dave 320FL MM 2 Greg 3 Matt ER (sName,address(Student)) ( (address) ( (sNumber > 1) (Student))) SELECT DISTINCT sName, address FROM Student; SELECT DISTINCT address FROM Student WHERE sNumber > 1; sName address Dave 320FL Greg Matt address 320FL

X Always Remember…. Only SELECT and FROM clauses are mandatory All the others are optional You can mix and match the optional ones But if you add a clause, then keep it in its order SELECT DISTINCT address FROM Student WHERE sNumber > 1; SELECT address FROM Student ORDER BY sNumber; SELECT address FROM Student WHERE sNumber > 1 ORDER BY sNumber; SELECT address FROM Student ORDER BY sNumber WHERE sNumber > 1; X

Aggregation + GroupBy

Possible Aggregations in SQL SELECT COUNT (*) FROM Student; SELECT COUNT (sNumber) FROM Student; SELECT MIN (sNumber) FROM Student; SELECT MAX (sNumber) FROM Student; SELECT SUM (sNumber) FROM Student; SELECT AVG (sNumber) FROM Student;

Grouping & Aggregation in SQL New optional clause called “GROUP BY” If the SELECT statement has “WHERE” Then WHERE conditions are evaluated first, then records are grouped SELECT pNumber, COUNT (sName) FROM Student GROUP BY pNumber; Form one group for each pNumber, and then count inside each group

GROUP BY: Example I Student cnt  count(*) (Student) sNumber sName address pNumber 1 Dave 320FL 2 Greg 3 Matt 4 Jan 500MA cnt  count(*) (Student) pNumber,cnt count(*) ( (sNumber > 1) (Student)) SELECT count(*) AS CNT FROM Student; SELECT pNumber, count(*) AS CNT FROM Student WHERE sNumber > 1 GROUP BY pNumber; CNT 4 pNumber CNT 1 2

GROUP BY: Example II Student sNumber sName address pNumber 1 Dave 320FL 2 Greg 3 Matt 4 Jan 500MA pNumber,address, CNT  count(sName), SUM  sum(sNumber) ( (sNumber > 1) (Student)) SELECT pNumber,address, count(sName) AS CNT, sum(sNumber) AS SUM FROM Student WHERE sNumber > 1 GROUP BY pNumber, address; pNumber address CNT SUM 1 320FL 2 3 500MA 4

Restrictions of GROUP BY If you group by A1, A2, …An, then any other column projected in SELECT clause must be inside an aggregation function SELECT pNumber, address, count(sName) AS CNT, sum(sNumber) AS SUM FROM Student WHERE sNumber > 1 GROUP BY pNumber, address; SELECT pNumber, address, sName, sum(sNumber) AS SUM FROM Student WHERE sNumber > 1 GROUP BY pNumber, address; X SELECT pNumber, count(sName) AS CNT, sum(sNumber) AS SUM FROM Student WHERE sNumber > 1 GROUP BY pNumber, address;

HAVING Clause: Putting Condition on Groups How to add conditions on each group? Select only the groups where the COUNT > 5 These conditions are after you build the groups (not before) Remember: WHERE conditions are executed before the groups are formed New optional clause called “HAVING”, added after the GROUP BY clause SELECT pNumber, COUNT (sName) FROM Student GROUP BY pNumber HAVING SUM(sNumber) > 2; Can reference aggregation inside HAVING

HAVING Clause: Example Student sNumber sName address pNumber 1 Dave 320FL 2 Greg 3 Matt 4 Jan 500MA  (SUM> 3) (pNumber,address, CNT  count(sName), SUM  sum(sNumber) ( (sNumber > 1) (Student))) SELECT pNumber,address, count(sName) AS CNT, sum(sNumber) AS SUM FROM Student WHERE sNumber > 1 GROUP BY pNumber, address HAVING SUM > 3; pNumber address CNT SUM 2 500MA 1 4

SELECT Statement Clauses SELECT <projection list> FROM <relation names> WHERE <conditions> GROUP BY <grouping columns> HAVING <grouping conditions> ORDER BY <order columns>; optional Optional clauses if added must be in the order above Order of execution FROM WHERE GROUP BY HAVING ORDER BY SELECT