Chapter 5-1 CSE 4701 In Class SQL Queries – Part II n Homework 3 from Spring 2015 n Problem 6.18 from the 6 th edition done in SQL and NOT relational expressions.

Slides:



Advertisements
Similar presentations
Exercise 2 Relational Calculus
Advertisements

Fundamentals of Database Systems Fourth Edition El Masri & Navathe
SQL DESIGN AND IMPLEMENTATION CONTENT SOURCES: ELAMSARI AND NAVATHE, FUNDAMENTALS OF DATABASE MANAGEMENT SYSTEMSELAMSARI AND NAVATHE, FUNDAMENTALS OF.
Copyright © 2004 Ramez Elmasri and Shamkant Navathe Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 15-1 Query Processing and.
COMPANY schema EMPLOYEE
OUTLINE OF THE LECTURE PART I GOAL: Understand the Data Definition Statements in Fig 4.1 Step1: Columns of the Tables and Data types. Step2: Single column.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
SQL Query Slides Sharif University Of Technology Database Systems CE 384 Prepared By: Babak Bagheri Hariri
Displaying Data from Multiple Tables. EMPNO DEPTNO LOC NEW YORK CHICAGO NEW YORK DALLAS
Database technology Lecture 2: Relational databases and SQL
Data Definition Languages Atif Farid Mohammad UNCC.
Company Database. CREATE TABLE DEPARMENT ( DNAME VARCHAR(10) NOT NULL, DNUMBER INTEGER NOT NULL, MGRSSN CHAR(9), MGRSTARTDATE CHAR(9), PRIMARY KEY (DNUMBER),
LAB 4 FIGURES.
Homework for November 2011 Nikolay Kostov Telerik Corporation
Employee database: Conceptual Schema in ERD Chapter 3, page 62.
Populating and Querying tables Insert and mostly View (DML)
Review Database Application Development Access Database Development ER-diagram Forms Reports Queries.
Northwind Sample database (also supplied with MS Access)
Announcements - Spring 2015 March 2, 2015
More SQL: Complex Queries, Triggers, Views, and Schema Modification 1.
관계 연산자 & SQL. Selection SELECT * FROM r WHERE A=B AND D>5.
SQL The relational DB Standard CS-450 Dr. Ali Obaidi.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1 CREATE/DROP/ALTER TABLE Data types : char, varchar, decimal, date CREATE TABLE DEPARTMENT.
Al-Imam University Girls Education Center Collage of Computer Science 1 ST Semester, 1432/1433H Chapter 8 Part 4 SQL-99 Schema Definition, Constraints,
Review: Application of Database Systems
Other SQL Query Examples
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
1 CSE 480: Database Systems Lecture 11: SQL. 2 SQL Query SELECT FROM WHERE –In MySQL, FROM and WHERE clauses are optional –Example:
Relational Algebra - Chapter (7th ed )
Retrieve the names of all employees in department 5 who work more than 10 hours per week on the ‘ProductX’ project. p10ssn ← (Π essn (σ hours > 10 (works-on.
 Employee (fname, minit, lname, ssn, bdate, address, sex, salary, superssn, dno)  Department (dname, dnumber, mgrssn, mgrstartdate) 
Database Management Systems. NESTING OF QUERIES  Some queries require that existing values in the database be retrieved and then used in a comparison.
Announcements Written Homework 1 due Friday –If you have fourth edition make sure you do the right problems Program 3 out today, due next Friday Nov 10.
莊裕澤資料庫基本概念 1 Yuh-Jzer Joung 莊 裕 澤 Dept. of Information Management National Taiwan University February, 2002 資料庫管理與資訊科技.
1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)
Slide 8- 1 THE HAVING-CLAUSE Provides a condition on the summary information Sometimes we want to retrieve the values of these functions for only those.
Structured Query Language
Structured Query Language (2) The main reference of this presentation is the textbook and PPT from : Elmasri & Navathe, Fundamental of Database Systems,
Monthly Sales Report 200X-Q1 XYZ Limited Co..
Announcements Written Homework 1 due Nov 2 –See course web page –Exercises 5.12, 5.15, 6.17, 6.20, 6.22 (a,c,f only). Today –continue with SQL (chapter.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
1Fundamentals of Database Systems 기본키에 밑줄을 그은 COMPANY 관계 데이타베이스 스키마 FNAMEMINITLNAMESSNBDATEADDRESSSEXSALARYSUPERSSNDNO EMPLOYEE DNAMEDNUMBERMGRSSNMGRSTARTDATE.
COMP163 Database Management Systems September 18, 2008 Lecture 8 – Sections SQL Queries.
IMS 4212: Intro to Multi-Table SELECT Statements 1 Dr. Lawrence West, MIS Dept., University of Central Florida Multi-Table SELECT Statements—Topics.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 (SQL Basics) Lecture # 9 July 7,2012.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus تنبيه : شرائح العرض.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
CS580 Advanced Database Topics Chapter 8 SQL Irena Pevac.
The SQL Database Grammar
CS580 Advanced Database Topics
Chapter 4 Basic SQL.
6/22/2018.
Retrieval Queries in SQL(DML)
Chapter 5 6e & 8 5e: Complex SQL
Subject Name: DATA BASE MANAGEMENT SYSTEM Subject Code: 10cs54
Chapter 4 6e & 8 5e: Basic SQL Prof. Steven A. Demurjian, Sr.
11/9/2018.
Outline: Relational Data Model
Joining Tables ضم الجداول وإستخراج مناظر views منها الهدف : 1- استخراج المعلومات من جدولين أو اكثر بالإستفادة من الرابط بينهما وبإستخدام SQL 2- شروط قواعد.
Company Requirements.
TEXAS SUPPLEMENT WAREHOUSE
SQL (more).
SQL-99: Schema Definition, Constraints, and Queries and Views
Advanced Joins IN ( ) Expression Subqueries with IN ( ) Expression
SQLPLUS: Oracle SQL Interface
SQL Grouping, Ordering & Arithmetics Presented by: Dr. Samir Tartir
SQL: Set Operations & Nested Queries. Presented by: Dr. Samir Tartir
OLTP Will be talking about On Line Transaction Processing OLTP for most of this course Operational databases As opposed to OLAP On Line Analytical Processing.
Answers to Midterm - Exam. Feb. 28, 2018
Presentation transcript:

Chapter 5-1 CSE 4701 In Class SQL Queries – Part II n Homework 3 from Spring 2015 n Problem 6.18 from the 6 th edition done in SQL and NOT relational expressions n Problem 4.10 in 6 th edition

Chapter 5-2 CSE 4701 Homework 3 from Spr 2015 n Problem 6.18 from the 6 th edition done in SQL and NOT relational expressions

Chapter 5-3 CSE 4701 c. Retrieve the names of all borrowers who do not have any books checked out SELECT Name FROM BORROWER B WHERE NOT EXIST ( SELECT * FROM BOOK_LOANS L WHERE B.CardNo = L.CardNo )

Chapter 5-4 CSE 4701 e. For each library branch, retrieve the branch name and the total number of books loaned out from that branch. SELECT L.BranchName, COUNT(*) FROM BOOK_COPIES B, LIBRARY_BRANCH L WHERE B.BranchId = L.BranchId GROUP BY L.BranchName

Chapter 5-5 CSE 4701 f. Retrieve the names, addr, and num of books checked out for all borrowers who have more than five books checked out. SELECT B.CardNo, B.Name, B.Address, COUNT(*) FROM BORROWER B, BOOK_LOANS L WHERE B.CardNo = L.CardNo GROUP BY B.CardNo HAVING COUNT(*) > 5

Chapter 5-6 CSE 4701 Homework 3 from Spr 2015 n Problem 4.10 in 6 th edition

Chapter 5-7 CSE 4701 Problem 4.10 in 6 th edition

Chapter 5-8 CSE 4701 (a) Retrieve the names of employees in department 5 who work more than 10 hours per week on the 'ProductX' project. SELECT LNAME, FNAME FROM EMPLOYEE WHERE DNO=5 AND SSN IN ( SELECT ESSN FROM WORKS_ON WHERE HOURS>10 AND PNO IN ( SELECT PNUMBER FROM PROJECT WHERE PNAME='ProductX' ) ) LNAME FNAME Smith John English Joyce

Chapter 5-9 CSE 4701 (e) Retrieve the names of employees who work on every project. SELECT LNAME, FNAME FROM EMPLOYEE WHERE NOT EXISTS ( SELECT PNUMBER FROM PROJECT WHERE NOT EXISTS ( SELECT * FROM WORKS_ON WHERE PNUMBER=PNO AND ESSN=SSN ) ) Result (empty):

Chapter 5-10 CSE 4701 (f) Retrieve the names of employees who do not work on any project. SELECT LNAME, FNAME FROM EMPLOYEE WHERE NOT EXISTS ( SELECT * FROM WORKS_ON WHERE ESSN=SSN ) Result (empty):

Chapter 5-11 CSE 4701 (i) Find the names/addrs of employees who work on at least one project in Houston but whose dept has no location in Houston. SELECT LNAME, FNAME, ADDRESS FROM EMPLOYEE WHERE EXISTS ( SELECT * FROM WORKS_ON, PROJECT WHERE SSN=ESSN AND PNO=PNUMBER AND PLOCATION='Houston' ) AND NOT EXISTS ( SELECT * FROM DEPT_LOCATIONS WHERE DNO=DNUMBER AND DLOCATION='Houston' ) Result: LNAME FNAME ADDRESS Wallace Jennifer 291 Berry, Bellaire, TX

Chapter 5-12 CSE 4701 (j) List the last names of dept managers who have no dependents. SELECT LNAME, FNAME FROM EMPLOYEE WHERE EXISTS ( SELECT * FROM DEPARTMENT WHERE SSN=MGRSSN ) ANDNOT EXISTS ( SELECT * FROM DEPENDENT WHERE SSN=ESSN ) Result: LNAME FNAME Borg James

ANN.13 CSE 4701 In Class SQL Queries – Part I Explaining Northwind Schema   Suppliers: A Suppliers Contact Info and web link.   Products: Names, suppliers, and Prices   Categories: Categories of Northwind products such as Beverages, Condiments, Confections, Dairy Products, Grains/Cereals, Meat/Poultry, Produce, Seafood   Orders: For each Customer with dates &Shipping   Order Details: Products, quantities, and price.   Employees: Typical Info.   Customers: Typical Info.   Shippers: Typical Info.

ANN.14 CSE 4701 Northwind Schema

ANN.15 CSE 4701 Northwind Schema – Key Types

ANN.16 CSE 4701 Prob 1: All Products Less that 10 return all columns. SELECT * FROM NORTHWIND.PRODUCTS WHERE UNITPRICE < 10;

ANN.17 CSE 4701 Prob 2: Prob 2: List products that is between 10 to 20 dollars by Price in Descending order SELECT * FROM NORTHWIND.PRODUCTS WHERE UNITPRICE >= 10 AND UNITPRICE <= 20 ORDER BY UNITPRICE DESC;

ANN.18 CSE 4701 Prob 3: List all the products in Beverages category, with its ProductID, Product Name, and Price, and CATGORYNAME by Price. SELECT PRODUCTID, PRODUCTNAME, UNITPRICE, CATEGORYNAME FROM NORTHWIND.PRODUCTS, NORTHWIND.CATEGORIESWHERE PRODUCTS.CategoryID = CATEGORIES.CategoryID AND Categories.CategoryName="Beverages“ ORDER BY UNITPRICE;

ANN.19 CSE 4701 Find and print the company names and company addresses of all Suppliers that supply the category name Seafood. Prob 4: Find and print the company names and company addresses of all Suppliers that supply the category name Seafood. SELECT DISTINCT suppliers.CompanyName, suppliers.Address FROM northwind.suppliers, northwind.categories, northwind.products WHERE suppliers.SupplierID = products.SupplierID AND categories.CategoryID = products.CategoryID AND categories.CategoryName = 'Seafood';

ANN.20 CSE 4701 Count and print the number of suppliers for each of the eight different categories of food Prob 5: Count and print the number of suppliers for each of the eight different categories of food SELECT categories.CategoryName, COUNT(suppliers.SupplierID) FROM northwind.categories, northwind.products, northwind.suppliers WHERE suppliers.SupplierID = products.SupplierID AND products.categoryID = categories.CategoryID GROUP BY categories.CategoryName;

ANN.21 CSE 4701 In Class SQL Queries Explaining Chinook Schema  Employees and Customers are Two Main DB Users  Repository Track Artists and Their Albums  Each Album has Multiple Tracks  Tracks are of a Particular Media and Genre  Customers can Also Create Playlists of Tracks  Customers have Invoices  Each Invoice has Multiple Lines  Each Line References one Track

ANN.22 CSE 4701 Chinook EER

ANN.23 CSE nook_Schema&referringTitle=Documentation nook_Schema&referringTitle=Documentation

ANN.24 CSE 4701 What Does Data Look Like? StevesSongs PL12, T3T3, Hello, A1 PL12, T5T4, Bye, A1 PL12, T7T5, MySong, A1 PL13, T3 etc.

ANN.25 CSE 4701 Prob 6: Customers and Employees that have the same last name and print out the Last Name (only once), Address, City, and State for each. SELECT employee.LastName, employee.Address, employee.City, employee.State, customer.Address, customer.city, customer.state FROM chinook.employee, chinook.customer WHERE employee.LastName = customer.LastName;

ANN.26 CSE 4701 Prob 7: Customer Names and Company Name of all Customers that have purchased a Rock Album. (where Name is ‘Rock’ in Genre table). SELECT DISTINCT customer.FirstName, customer.LastName, customer.Company FROM chinook.customer, chinook.invoice, chinook.invoiceline, chinook.track, chinook.genre WHERE customer.CustomerId = invoice.CustomerId AND invoice.InvoiceId = invoiceline.InvoiceId AND invoiceline.TrackId = track.TrackId AND track.GenreId = genre.GenreId AND genre.name = 'rock';

ANN.27 CSE 4701 Prob 8: Album name and tracks of all of the albums by the composer James Hetfield, grouped by Album.  Find all of print album name and tracks of all of the albums by the composer James Hetfield, grouped by Album  What Does Data for Track Look Like? SELECT NAME, COMPOSER FROM CHINOOK.TRACK;

ANN.28 CSE 4701 Prob 8: Album name and tracks of all of the albums by the composer James Hetfield, grouped by Album.  There are other Spellings to Consider:  J. Hetfield  There are some Hetfields without First Name or Initial  What SQL Command is Needed for Searching Composer?  LIKE is Used to Compare Partial Strings  '%' (or '*') Replaces an Arbitrary # of characters '_' replaces a single arbitrary character SELECT FNAME, LNAME FROM EMPLOYEE WHERE ADDRESS LIKE '%Houston,TX% '

ANN.29 CSE 4701 Prob 8: Album name and tracks of all of the albums by the composer James Hetfield, grouped by Album. SELECT album.title, track.name FROM chinook.album, chinook.track WHERE album.AlbumId = track.AlbumId AND track.Composer LIKE '%Hetfield%';

ANN.30 CSE 4701 Prob 9: customer in Canada find all invoices and for each, print Customer Name, number of invoices for customer, and the total amount paid for all invoices. SELECT customer.LastName, COUNT(invoice.InvoiceId), SUM(invoice.total) FROM chinook.customer, chinook.invoice WHERE customer.Country = 'Canada' AND customer.CustomerId = invoice.CustomerId GROUP BY customer.LastName;