Displaying Data from Multiple Tables. EMPNO DEPTNO LOC ----- ------- -------- 7839 10 NEW YORK 7698 30 CHICAGO 7782 10 NEW YORK 7566 20 DALLAS 7654 30.

Slides:



Advertisements
Similar presentations
COMPANY schema EMPLOYEE
Advertisements

Basic Queries. 2 Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not the.
SQL Query Slides Sharif University Of Technology Database Systems CE 384 Prepared By: Babak Bagheri Hariri
Company Database. CREATE TABLE DEPARMENT ( DNAME VARCHAR(10) NOT NULL, DNUMBER INTEGER NOT NULL, MGRSSN CHAR(9), MGRSTARTDATE CHAR(9), PRIMARY KEY (DNUMBER),
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
1 CSE 480: Database Systems Lecture 10: SQL - DML Reference: Read Chapter 4 of the textbook.
METU Department of Computer Eng Ceng 302 Introduction to DBMS SQL-99: Schema Definition, Basic Constraints, and Queries by Pinar Senkul resources: mostly.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
CS 104 Introduction to Computer Science and Graphics Problems Introduction to Database (2) Basic SQL 12/05/2008 Yang Song.
Database Systems Chapter 8 ITM 354. Data Definition, Constraints, and Schema Changes Used in SQL to Create, Drop, and Alter the descriptions of the tables.
관계 연산자 & 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 Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
FEN  Queries: SELECT  Data Manipulation: INSERT, UPDATE, DELETE SQL: Structured Query Language – Part 2.
SQL: Schema Definition, Constraints, & Queries and Views.
Relational Algebra - Chapter (7th ed )
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL.
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
 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.
Session 2: SQL (A): Parts 1 and 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
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.
Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc.
莊裕澤資料庫基本概念 1 Yuh-Jzer Joung 莊 裕 澤 Dept. of Information Management National Taiwan University February, 2002 資料庫管理與資訊科技.
SQL Chapter 8. SQL or SEQUEL - (Structured English Query Language) Based on relational algebra Developed in 1970's released in early 1980's Standardized.
Chapter 7 SQL HUANG XUEHUA. AGGREGATE FUNCTIONS Include COUNT, SUM, MAX, MIN, and AVG Query 15: Find the maximum salary, the minimum salary, and the.
Chapter 8 Part 2 SQL-99 Schema Definition, Constraints, Queries, and Views.
Copyright س Oracle Corporation, All rights reserved. 4 Displaying Data from Multiple Tables.
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.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Chapter 11 SQL DML. Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not.
1 Chapter 5 The SQL Language Data Definition Language The Data Definition Language (DDL) is used to create and destroy databases and database.
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.
Chapter SQL DML. Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not the.
COMP163 Database Management Systems September 18, 2008 Lecture 8 – Sections SQL Queries.
ISC321 Database Systems I Chapter 5: SQL: Advanced Queries, Assertions, Triggers, and Views Fall 2015 Dr. Abdullah Almutairi.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 (SQL Basics) Lecture # 9 July 7,2012.
Copyright © 2004 Pearson Education, Inc. Schema Definition, Basic Constraints, and Queries.
SQL- Updates, Assertions and Views. Data Definition, Constraints, and Schema Changes Used to CREATE, DROP, and ALTER the descriptions of the tables (relations)
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Displaying Data from Multiple Tables. Objectives After completing this lesson, you should be able to do the following: –Write SELECT statements to access.
4 Displaying Data from Multiple Tables. 4-2 Objectives At the end of this lesson, you should be able to: Write SELECT statements to access data from more.
Structured Query Language (3) The main reference of this presentation is the textbook and PPT from : Elmasri & Navathe, Fundamental of Database Systems,
CS580 Advanced Database Topics Chapter 8 SQL Irena Pevac.
1 Chapter (8) SQL The Relational Database Standard Objectives Presenting the main features of SQL SQL is the Structured Query Language SQL commands SQL.
The SQL Database Grammar
CS580 Advanced Database Topics
SQL: SchemaDefinition, Constraints, and Queries and Views
Chapter 4 Basic SQL.
6/22/2018.
CS580 Advanced Database Topics
Retrieval Queries in SQL(DML)
Subject Name: DATA BASE MANAGEMENT SYSTEM Subject Code: 10cs54
376a. Database Design Dept. of Computer Science Vassar College
11/9/2018.
Company Requirements.
Session - 6 Sequence - 2 SQL: The Structured Query Language:
Chapter 4 & 5 Basic SQL More SQL
Chapter 4 Basic SQL. Chapter 4 Basic SQL Chapter 4 Outline Overview of SQL SQL Data Definition (DDL) for Specifying a Relational Database Schema CREATE.
Structured Query Language (3)
SQL-99: Schema Definition, Constraints, and Queries and Views
ISC321 Database Systems I Chapter 4: SQL: Data definition, Constraints, and Basic Queries and Updates Fall 2015 Dr. Abdullah Almutairi.
SQL Updating Database Contents Presented by: Dr. Samir Tartir
SQL Grouping, Ordering & Arithmetics Presented by: Dr. Samir Tartir
Presentation transcript:

Displaying Data from Multiple Tables

EMPNO DEPTNO LOC NEW YORK CHICAGO NEW YORK DALLAS CHICAGO CHICAGO rows selected. EMPNO DEPTNO LOC NEW YORK CHICAGO NEW YORK DALLAS CHICAGO CHICAGO rows selected. EMPDEPT EMPNOENAME...DEPTNO KING BLAKE MILLER DEPTNO DNAME LOC ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON Obtaining Data from Multiple Tables

Displaying Data from Multiple Tables What is a join? When data from more than one table in the database is required. A join condition is used. Rows in one table can be joined to rows in another table according to common values existing in corresponding columns, usually primary and foreign key columns.

Guidelines: When writing a SELECT statement that joins tables. Precede the column name with the table name for clarity and to enhance database access. Write the join condition in the where clause. If the same column name appears in more than one table, the column name must be prefixed with the table name. To join n tables together, you need minimum of (n-1) join conditions. Therefore, to join four tables, a minimum of three joins are required. This rule may not apply if your table has a concatenated primary key, in which case more than one column is required to uniquely identify each row.

Query 1 Retrieve the name and address of all employees who work for the 'Research' department SELECT FNAME, LNAME, ADDRESS FROM EMPLOYEE, DEPARTMENT WHERE DNAME='Research' AND DNUMBER=DNO;

Query 2 For every project located in 'Stafford', list the project number, the controlling department number, and the department manager's last name, address, and birthdate. SELECT PNUMBER, DNUM, LNAME, BDATE, ADDRESS FROMPROJECT, DEPARTMENT, EMPLOYEE WHERE DNUM=DNUMBER AND MGRSSN=SSN AND PLOCATION='Stafford'

Query 3 The name of the females employees and the name of their male dependents. SELECT fname, lname,d.dependent_name FROM employee e, dependent d WHERE ssn=d.ESSN and d.sex='M' and e.sex='F'

Query 4 All managers first and last names who begun managing the department during 1981 and 1991 select fname, lname from employee, department where mgrssn=ssn and (mgrstartdate between '01-jan-1981' and '31-dec-1991')

Query 5 Retrieve Employee name and the project name of all employees who worked on a project 10 to 30 hours. SELECT fname, lname,pname FROM employee, works_on, project WHERE pnumber=pno and essn=ssn and (hours between 10 and 30 )

Query 6 List the project names and the total hours the employees have spent on each. SELECT pname, sum(hours) FROM works_on, project WHERE pno=pnumber GROUP BY pname

Query 7 For each department that has more that 2 employees, retrieve the department name and the average salary of all employees working in that department arranged in descend order by the average salary. SELECT dname, count(*), avg(salary) FROM employee, department WHERE dno=dnumber GROUP BY dname HAVING count(*)>=2 ORDER BY avg(salary) desc

Query 8 Retrieve the names of all employees who have more than two dependents SELECT fname, lname, count(*) "Number of dependents" FROM employee, dependent WHERE ssn=essn GROUP BY fname, lname HAVING count(*) >2;

Self joins Joining table to itself Consider the situation, where we want to display the name of Smith’s supervisor, you need to: Find Smith in the employee table by looking at the LNAME column. Find the supervisor number for Smith by looking at the SUPERSSN column. Smith’s supervisor SSN is Find the name of the supervisor with SSN by looking at LNAME column. Franklin SSN is , so Franklin is Smith’s manager.

Query 9 For each employee, retrieve the employee's name, and the name of his or her immediate supervisor SELECTE.FNAME, E.LNAME, S.FNAME, S.LNAME FROM EMPLOYEE E, EMPLOYEE S WHEREE.SUPERSSN=S.SSN

Example of table aliases Retrieve the name and address of all employees who work for the 'Research' department SELECT E.FNAME, E.LNAME, E.ADDRESS FROM EMPLOYEE E, DEPARTMENT D WHERE D.DNAME='Research' AND D.DNUMBER=E.DNO;

The DELETE Command Removes rows from a relation Includes a WHERE-clause to select the rows to be deleted rows are deleted from only one table at a time A missing WHERE-clause specifies that all rows in the relation are to be deleted; the table then becomes an empty table The number of rows deleted depends on the number of rows in the relation that satisfy the WHERE-clause Referential integrity should be enforced

The DELETE Command DELETE FROM EMPLOYEE WHERELNAME='Brown’ DELETE FROM EMPLOYEE WHERE SSN=' ’ DELETE FROM EMPLOYEE

The UPDATE Command Used to modify attribute values of one or more selected rows A WHERE-clause selects the rows to be modified An additional SET-clause specifies the attributes to be modified and their new values Each command modifies rows in the same relation Referential integrity should be enforced

The UPDATE Command  Change the location and controlling department number of project number 10 to 'Bellaire' and 5, respectively.  UPDATE PROJECT SETPLOCATION = 'Bellaire', DNUM = 5 WHEREPNUMBER=10