Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database.

Slides:



Advertisements
Similar presentations
Union, Intersection, Difference (subquery) UNION (subquery) produces the union of the two relations. Similarly for INTERSECT, EXCEPT = intersection and.
Advertisements

© Abdou Illia MIS Spring 2014
Chapter 4 Hotel (hotelno, hotelname, city)
1 Assignment 2 Relational Algebra Which tables? What operations? Common attributes? What result (attributes)? Syntax (Standard Notations and Symbols) –Product:
COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data.
CS 3630 Database Design and Implementation. SQL Query Clause Select and From Select * From booking; select hotel_no, guest_no, room_no from booking; select.
Information Resources Management February 27, 2001.
Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.
Chapter 6 SQL. Agenda Data Definition Language (DDL) Access Control.
Chapter 6 SQL Homework.
Chapter 5 SQL Homework.
Using SQL to create tables Ways of using Databases.
Project Phase I Phase II Due Monday, April 15 Groups 1.
Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database.
Relational Operators, SQL, and Access Query ISYS 562.
1 Announcements Read 6.7 – 6.10 for Friday Homework 6, due Friday 10/29 Research paper –List of sources - due 10/29 Department Seminar –The Role of Experimentation.
Chapter 6 SQL. Agenda Data Definition Language (DDL) Access Control.
Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database.
Chapter 10 Data and Knowledge Management. Agenda Information processing Database Data Administrator The DBMS Distributing data Data warehousing and data.
Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform.
Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29.
Oracle Database Administration Lecture 2 SQL language.
BACS--485 SQL 11 BACS 485 Structured Query Language.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) Structured Query Language Introduction to SQL Structured Query Language.
Using Special Operators (LIKE and IN)
SQL queries ordering and grouping and joins
MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 5 © Akhilesh Bajaj, 2000, 2002, 2003, All.
BACS 287 Structured Query Language 1. BACS 287 Visual Basic Table Access Visual Basic provides 2 mechanisms to access data in tables: – Record-at-a-time.
SQL - DML. Data Manipulation Language(DML) Are used for managing data: –SELECT retrieve data from the a database –INSERT insert data into a table –UPDATE.
Chapter 5 SQL: Data Manipulation Thomas Connolly, Carolyn Begg, Database System, A Practical Approach to Design Implementation and Management, 4 th Edition,
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
SQL introduction 2013.
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Chapter 6 SQL. Agenda Data Definition Language (DDL) Access Control.
(C) 2000, The University of Michigan 1 Database Application Design Handout #5 February 4, 2000.
CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
SCUHolliday - coen 1787–1 Schedule Today: u Subqueries, Grouping and Aggregation. u Read Sections Next u Modifications, Schemas, Views. u Read.
INF 280 Database Systems SQL:Join
CS 3630 Database Design and Implementation. Base Table and View Base Table Stored on disk View Virtual table Records are not stored on disk Query is stored.
CS 3630 Database Design and Implementation. Where Clause and Aggregate Functions -- List all rooms whose price is greater than the -- average room price.
April 2002 Information Systems Design John Ogden & John Wordsworth 1 Database Design SQL (1) John Wordsworth Department of Computer Science The University.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL: Data Manipulation. Objectives Describe the purpose and importance of SQL. Demonstrate how to retrieve data from database using SELECT and: ▫Use compound.
CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,
CS 3630 Database Design and Implementation. Joins Retrieve data from two or more tables Join Conditions PK and FK (Natural Join) Other attributes (Theta.
Database Design lecture 3_2 Slide 1 Database Design Lecture 3_2 Data Manipulation in SQL Simple SQL queries References: Text Chapter 8 Oracle SQL Manual.
CS 3630 Database Design and Implementation
Assignment 2 Relational Algebra Which tables? What operations?
Structured Query Language
CS 3630 Database Design and Implementation
CS 3630 Database Design and Implementation
References: Text Chapters 8 and 9
Thomas M. Connolly Carolyn E. Begg
Assignment 2.
SQL Pertemuan 6 9/17/2018 Sistem Basis Data.
CS 3630 Database Design and Implementation
Web Services שפת SQL כתבה: זהבה יעקובסון ליווי מקצועי : ארז קלר
Chapter Name SQL: Data Manipulation
CS 3630 Database Design and Implementation
Chapter 4 Summary Query.
CS 3630 Database Design and Implementation
Database systems Lecture 3 – SQL + CRUD
Contents Preface I Introduction Lesson Objectives I-2
SQL: Structured Query Language
Presentation transcript:

Chapter 5 SQL

Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

SQL DML - SELECT SELECT [DISTINCT|ALL] {* | [col- expr [AS newname]][,...] FROM table-name [alias] [,...] [WHERE condition] [GROUP BY colm [, colm] [HAVING condition]] ORDER BY colm [, colm]

SQL DML - SELECT SELECT attributes (or calculations: +, -, /, *) FROM relation SELECT DISTINCT attributes FROM relation

Examples SELECT stuname FROM student; SELECT stuid, stuname, credit FROM student; SELECT stuid, stuname, credit+10 FROM student; SELECT DISTINCT major FROM student;

SQL DML - SELECT SELECT attributes (or * wild card) FROM relation WHERE condition

Examples SELECT * FROM student; SELECT stuname, major, credit FROM student WHERE stuid = ‘S114’; SELECT * FROM faculty WHERE dept = ‘MIS’;

SELECT - WHERE condition AND OR NOT IN NOT INBETWEEN IS NULLIS NOT NULL SOMEALL LIKE '%' multiple characters LIKE ‘_’ single character

Examples SELECT * FROM faculty WHERE dept = ‘MIS’ AND rank = ‘full professor’; SELECT * FROM faculty WHERE dept = ‘MIS’ OR rank = ‘full professor’; SELECT * FROM faculty WHERE dept = ‘MIS’ NOT rank = ‘full professor’;

SELECT * FROM class WHERE room LIKE ‘B_S%’; SELECT * FROM class WHERE room NOT LIKE ‘BUS%’; SELECT productid, productname FROM inventory WHERE onhand BETWEEN 50 and 100;

SELECT companyid, companyname FROM company WHERE companyname BETWEEN ‘G’ AND ‘K’; SELECT productid, productname FROM inventory WHERE onhand NOT BETWEEN 50 and 100; SELECT companyid, companyname FROM company WHERE companyname NOT BETWEEN ‘G’ AND ‘K’;

SELECT facname FROM faculty WHERE dept IN (‘MIS’, ‘ACT’); SELECT facname FROM faculty WHERE rank NOT IN (‘assistant’, ‘lecture’); SELECT customername FROM customer WHERE add IS NOT NULL;

SELECT customername FROM customer WHERE creditlimit IS NULL;

SELECT - aggregate functions COUNT (*) COUNT SUM AVG MIN MAX

Examples SELECT COUNT(*) FROM student; SELECT COUNT(major) FROM student; SELECT COUNT(DISTINCT major) FROM student;

SELECT COUNT(stuid), SUM(credit), AVG(credit), MAX(credit), MIN(credit) FROM student;

How many different guests have made bookings for August 2004? Hotel (hotelno, hotelname, city) Room (roomno, hotelno, type, price) Booking (hotelno, guestno, datefrom, dateto, roomno) Guest (guestno, guestname, guestaddress)

How many different guests have made bookings for August 2004? SELECT COUNT(DISTINCT guestno) FROM booking WHERE (datefrom <= ‘8/31/04’) AND (dateto >= ‘8/1/04’);

SELECT - GROUP GROUP BY HAVING

Examples SELECT major, AVG(credit) FROM student GROUP BY major SELECT course#, COUNT(stuid) FROM enrollment GROUP BY course#

Examples SELECT major, AVG(credit) FROM student GROUP BY major HAVING COUNT(*) > 2; SELECT course#, COUNT(stuid) FROM enrollment GROUP BY course# HAVING COUNT(*) > 2;

SELECT major, AVG(credit) FROM student WHERE major IN (‘mis’, ‘act’) GROUP BY major HAVING COUNT(*) > 2;

SELECT - ORDER BY ORDER BY ORDER BY... DESC

Examples SELECT facname, rank FROM faculty ORDER BY facname; SELECT facname, rank FROM faculty ORDER BY rank DESC, facname;

SELECT - JOIN Tables Multiple tables in FROM clause MUST have join conditions!!!

Examples SELECT stuname, grade FROM student, enrollment WHERE student.stuid = enrollment.stuid;

SELECT enrollment.course#, stuname, major FROM class, enrollment, student WHERE class.course# = enrollment.course# AND enrollment.stuid = student.stuid AND facid = ‘F114’ ORDER BY enrollment.course#;

OUTER JOINS RIGHT JOIN LEFT JOIN FULL JOIN Appending (+) to the optional column (null) in the join condition (Oracle)

Examples SELECT f.facid, f.facname, c.course# FROM class c, faculty f WHERE c.facid (+) = f.facid ORDER BY f.facname; (rightouterjoin) SELECT s.stuname, major, grade FROM student s, enrollment e WHERE s.stuid = e.stuid (+) ORDER BY s.stuname; (leftouterjoin)

SELECT f.facid, f.facname, c.course# FROM class c RIGHT JOIN faculty f ON c.facid = f.facid ORDER BY f.facname; (rightouterjoin) SELECT s.stuname, major, grade FROM student s LEFT JOIN enrollment e ON s.stuid = e.stuid ORDER BY s.stuname; (leftouterjoin)

SELECT f.facid, f.facname, c.course#, c.room FROM class c, faculty f WHERE c.facid (+) = f.facid (+) ORDER BY f.facname; (fullouterjoin)

SELECT f.facid, f.facname, c.course#, c.room FROM class c FULL JOIN faculty f ON c.facid = f.facid ORDER BY f.facname; (fullouterjoin)

Example List the number of room in each hotel in London. Hotel (hotelno, hotelname, city) Room (roomno, hotelno, type, price) Booking (hotelno, guestno, datefrom, dateto, roomno) Guest (guestno, guestname, guestaddress)

Example List the number of room in each hotel in London. SELECT r.hotelno, COUNT(roomno) FROM room r, hotel h WHERE r.hotelno=h.hotelno AND city = ‘London' GROUP BY hotelno;

Union Compatible Operations UNION MINUS or EXCEPT INTERSECT Union compatible operator [ALL] [CORRESPONGIND][BY column,..] (ALL includes duplicated rows in the result) Used between SELECT commands

Examples SELECT stuid, stuname FROM sacstudent UNION SELECT stuid, stuname FROM chicostudent; SELECT * FROM sacstudent UNION CORRESPONGIND BY stuid, stuname SELECT * FROM chicostudent;

SELECT stuid, stuname FROM sacstudent EXCEPT SELECT stuid, stuname FROM chicostudent; (SELECT stuid, stuname FROM sacstudent) INTERSECT (SELECT stuid, stuname FROM chicostudent) ORDER BY 2;

Column Alias SELECT prodid, prodname, (salesprice - goodofcost) profit FROM product ORDER BY prodid; SELECT prodid, prodname, (salesprice - goodofcost) AS profit FROM product ORDER BY prodid;

SUBQUERY SELECT stuid, stuname, credit FROM student WHERE credit > (SELECT AVG(credit) FROM student);

SELECT stuid, stuname, major FROM student WHERE stuid IN (SELECT stuid FROM enrollment);

SELECT stuid, stuname, major FROM student WHERE stuid NOT IN (SELECT stuid FROM enrollment)

Example What is the most commonly booked room type for all hotels in London? Hotel (hotelno, hotelname, city) Room (roomno, hotelno, type, price) Booking (hotelno, guestno, datefrom, dateto, roomno) Guest (guestno, guestname, guestaddress)

What is the most commonly booked room type for all hotels in London? SELECT type, MAX(y) FROM (SELECT type, COUNT(type) AS y FROM booking b, hotel h, room r WHERE r.roomno = b.roomno AND r.hotelno = b.hotelno AND b.hotelno = h.hotelno AND city = 'London' GROUP BY type) GROUP BY type;

EXIST SELECT s.stuname, major FROM student s WHERE EXIST (SELECT * FROM enrollment e WHERE s.stuid = e.stuid);

NOT EXIST SELECT s.stuname, major FROM student s WHERE NOT EXIST (SELECT * FROM enrollment e WHERE s.stuid = e.stuid);

SOME SELECT stuid, stuname, major, credit FROM student WHERE credit > SOME (SELECT credit FROM student WHERE major=‘mis’);

ANY SELECT stuid, stuname, major, credit FROM student WHERE credit > ANY (SELECT credit FROM student WHERE major=‘mis’);

ALL SELECT stuid, stuname, major, credit FROM student WHERE credit > ALL (SELECT credit FROM student WHERE major=‘mis’);

What is the lost income from unoccupied rooms at the Grosvenor Hotel today? Hotel (hotelno, hotelname, city) Room (roomno, hotelno, type, price) Booking (hotelno, guestno, datefrom, dateto, roomno) Guest (guestno, guestname, guestaddress)

What is the lost income from unoccupied rooms at the Grosvenor Hotel today? SELECT SUM(price) FROM room r, hotel h WHERE r.hotelno = h.hotelno AND h.hotelname = 'Grosvenor’ AND r.roomno NOT IN (SELECT roomno FROM booking b, hotel h WHERE b.hotelno = h.hotelno AND (datefrom <= ‘SYSTEM DATE’ AND dateto >= ‘SYSTEM DATE’) AND h.hotelname = 'Grosvenor');

What is the lost income from unoccupied rooms at each hotel today? Hotel (hotelno, hotelname, city) Room (roomno, hotelno, type, price) Booking (hotelno, guestno, datefrom, dateto, roomno) Guest (guestno, guestname, guestaddress)

What is the lost income from unoccupied rooms at each hotel today? SELECT h.hotelno, SUM(price) FROM room r WHERE roomno NOT EXIST (SELECT * FROM booking b, hotel h, room r WHERE b.hotelno = h.hotelno AND r.roomno = b.roomno AND r.hotelno = b.hotelno AND datefrom <= ‘SYSTEM DATE’ AND dateto >= ‘SYSTEM DATE’ ) GROUP BY hotelno;

SQL DML - UPDATE, INSERT, DELETE INSERT INTO table-name [(colm [, colm])] VALUES (const [, const] ) UPDATE table-name SET colm = expr [colm = expr]... [WHERE condition] DELETE FROM table-name [WHERE condition]

Examples INSERT INTO student (stuid, stuname, major, credit) VALUES (‘S114’, ‘Grace’, ‘MIS’, 60); UPDATE student SET major = ‘Database’, credit = 100 WHERE stuid = ‘S114’; UPDATE student SET major = ‘MIS’;

DELETE FROM student WHERE stuid = ‘S114’; DELETE FROM student;

Points To Remember Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

Assignment Review chapter 1 – 5, appendix C Read chapter 6 Homework Assignment –5.7 – 5.28 (not 5.18) –Due date: