SQL for Data Retrieval. Review Questions of Previous Class Q1. Show the sum of hours worked for project with ID 1200 (use ASSIGNMENT table) – Use “SUM”

Slides:



Advertisements
Similar presentations
SQL – Part 2 Multiple Tables CIS 324 – Chapter 5.
Advertisements

4d. Structured Query Language – JOIN Operation Lingma Acheson Department of Computer and Information Science IUPUI CSCI N207 Data Analysis with Spreadsheets.
SQL-week5-1 In-Class Exercise Answer IST 210 Organization of Data IST2101.
Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete.
Structured Query Language Chapter Three Part 3 – Inserts, Updates, Deletes.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Introduction to Structured Query Language (SQL)
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Structured Query Language (SQL)
SQL Views Chapter 3A DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 5 th Edition.
SQL for Data Retrieval. Save your SQL Scripts When working with SQL Management Studio, you should keep saving your scripts as a.sql file to somewhere.
+ Structured Query Language Part 2 KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall.
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Download three SQL script files from wiki page.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu
15 Structured Query Language (SQL). 2 Objectives After completing this section, you should be able to: Understand Structured Query Language (SQL) and.
CHAPTER 7 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
SQL – Structured Query Language CIS 324 – Chapter 5.
SQL Views Appendix E DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
Copyright © 2004, Oracle. All rights reserved. Lecture 3: Creating Other Schema Objects Lecture 3: Creating Other Schema Objects ORACLE.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Using Special Operators (LIKE and IN)
SQL Views Chapter 3A DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
Structured Query Language SQL-I
Structured Query Language (SQL) IST2101. Structured Query Language – Acronym: SQL – Pronounced as “S-Q-L” [“Ess-Que-El”] – Originally developed by IBM.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
Databases MIS 21. Some database terminology  Database: integrated collection of data  Database Management System (DBMS): environment that provides mechanisms.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Recap of SQL Lab no 8 Advance Database Management System.
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Select your database – Your database name is.
Chapter 2 Views. Objectives ◦ Create simple and complex views ◦ Creating a view with a check constraint ◦ Retrieve data from views ◦ Data manipulation.
STRUCTURED QUERY LANGUAGE SQL-II IST 210 Organization of Data IST210 1.
SQL Basic. What is SQL? SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database.
STRUCTURED QUERY LANGUAGE SQL-III IST 210 Organization of Data IST210 1.
CREATE TABLE CREATE TABLE statement is used for creating relations Each column is described with three parts: column name, data type, and optional constraints.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
INCLUDING CONSTRAINTS lecture5. Outlines  What are Constraints ?  Constraint Guidelines  Defining Constraint  NOT NULL constraint  Unique constraint.
SQL for Data Retrieval. Save your SQL Scripts When working with SQL Management Studio, you should keep saving your scripts as a.sql file to somewhere.
SQL Views Chapter 3A. Appendix Objectives Learn basic SQL statements for creating views Learn basic SQL statements for using views Understand the reasons.
SQL-5 In-Class Exercise Answer IST 210 Organization of Data IST2101.
Structured Query Language
DML Part 1 Yogiek Indra Kurniawan. All Files Can Be Downloaded at : Menu : “Perkuliahan”
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
WEEK# 12 Haifa Abulaiha November 02,
Including Constraints. What Are Constraints? Constraints enforce rules at the table level. You can use constraints to do the following: – Enforce rules.
Drill Consider the following tables with the following fields: Student: FName, LName, StudentID, Age, Yr, Course Grades: ID, P1, P2, P3 1.Display the.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
MIDTERM REVIEW IST 210 Organization of Data IST210 1.
Structured Query Language SQL-II IST 210 Organization of Data IST2101.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
Structured Query Language SQL-I IST 210 Organization of Data IST2101.
Structured Query Language
3d. Structured Query Language – JOIN Operations
Lecturer: Mukhtar Mohamed Ali “Hakaale”
Chapter 2 Views.
“Manipulating Data” Lecture 6.
Structured Query Language
“Manipulating Data” Lecture 6.
Chapter 2 Views.
IST 318 Database Administration
Manipulating Data Lesson 3.
Presentation transcript:

SQL for Data Retrieval

Review Questions of Previous Class Q1. Show the sum of hours worked for project with ID 1200 (use ASSIGNMENT table) – Use “SUM” and “WHERE” slide #9 (3-SQL-4.pptx) Q2. Count how many distinct departments in project table – Use “COUNT” slide #9 (3-SQL-4.pptx) – Use “DISTINCT” IST2102

Review Questions of Previous Class Q3. Group projects by departments. Show department names and number of projects associated with each department – Use “group by”, slide #13 (3-SQL-4.pptx) Q4. Add a constraint to the question above. Only show the departments with number of projects more than 1 – Use ”group by” and “having”, slide #13 (3-SQL-4.pptx) IST2103

Retrieving Information from Multiple Tables Two approaches – Subqueries – Joins

Example 1: Querying Two Tables IST2105 Show the names of employees who worked less than 20 hours Q2. Get names of the employees with employee number 4, 5: Tom Caruthers, Heather Jones Q1. Check “worked less than 20 hours” in ASSIGNMENT table: employee number 4, 5 EMPLOYEE ASSIGNMENT

Example 1: Use Subquery IST2106 SELECTEmployeeNumber FROMASSIGNMENT WHEREHoursWorked < 20; SELECTFirstName, LastName FROMEMPLOYEE WHEREEmployeeNumber IN (4, 5); SELECTFirstName, LastName FROMEMPLOYEE WHEREEmployeeNumber IN (SELECTEmployeeNumber FROMASSIGNMENT WHEREHoursWorked < 20); Show the names of employees who worked less than 20 hours

Exercise 1: Use Subquery IST2107 Show the names of employees who is assigned to ProjectID 1000 – First write two separate queries and then merge into one query 7 Step 2. Show the names of employees with the employee numbers from Q1 Q1. Find employee numbers who is assigned to ProjectID 1000 EMPLOYEE ASSIGNMENT

Example 1: Use Join IST2108 SELECTFirstName, LastName, HoursWorked FROMEMPLOYEE AS E, ASSIGNMENT AS A WHEREE.EmployeeNumber = A.EmployeeNumber ANDHoursWorked < 20; SELECTEmployeeNumber FROMASSIGNMENT WHEREHoursWorked < 20; SELECTFirstName, LastName FROMEMPLOYEE WHEREEmployeeNumber IN (4, 5); Show the names of employees who worked less than 20 hours SELECTFirstName, LastName FROMEMPLOYEE WHEREEmployeeNumber IN (SELECTEmployeeNumber FROMASSIGNMENT WHEREHoursWorked < 20); Show columns from multiple tables

Example 1: Use Join IST2109 SELECTFirstName, LastName, HoursWorked FROMEMPLOYEE AS E, ASSIGNMENT AS A WHEREE.EmployeeNumber = A.EmployeeNumber ANDHoursWorked < 20; Show the names of employees who worked less than 20 hours Shared column: EmployeeNumber

Exercise 1: Use Join Show the names of employees who is assigned to ProjectID 1000 IST21010 Shared column: EmployeeNumber

Exercise 2: Use Subquery Show the project names assigned to EmployeeNumber 4 IST21011 ASSIGNMENT PROJECT

Exercise 2: Use Join Show the project names assigned to EmployeeNumber 4 IST21012 Shared column: ProjectID

Example 2: Querying Three Tables Show the names of employees who are assigned with projects associated with Finance department IST21013 Names of employees Shared column: EmployeeNumber Associated with Finance department Shared column: ProjectID

Example 2: Use Subquery IST21014 Show the names of employees who are assigned with projects associated with Finance department ASSIGNMENT PROJECT Q1. Associated with Finance department: 1100, 1400 Q3. Employee names with number in {4,6,4,5,6} EMPLOYEE Q2. EmployeeNumber assigned to project 1100 or 1400: 4,6,4,5,6

Example 2: Use Subquery IST21015 SELECTEmployeeNumber FROMASSIGNMENT WHEREProjectID IN (1100, 1400); SELECTFirstName, LastName FROMEMPLOYEE WHEREEmployeeNumber IN (4, 5, 6); SELECTFirstName, LastName FROMEMPLOYEE WHEREEmployeeNumber IN (SELECTEmployeeNumber FROMASSIGNMENT WHEREProjectID IN (SELECTProjectID FROMPROJECT WHEREDepartment = 'Finance' ) ); Show the names of employees who are assigned with projects associated with Finance department SELECTProjectID FROMPROJECT WHEREDepartment = 'Finance';

Example 2: Use Join IST21016 SELECTDISTINCT FirstName, LastName FROMEMPLOYEE AS E, PROJECT AS P, ASSIGNMENT AS A WHEREE.EmployeeNumber = A.EmployeeNumber ANDP.ProjectID = A.ProjectID ANDP.Department = 'Finance'; Show the names of employees who are assigned with projects associated with Finance department Shared column: ProjectID Shared column: EmployeeNumber

Exercise 3: Use Subquery Show all the project names that are assigned to Ken – Write 3 separate queries first and then merge them into 1 IST21017 ASSIGNMENT PROJECT Q3. Names of the projects with ID in (1000, 1300) Q1. Employee numbers with first name as Ken: 10 EMPLOYEE Q2. ProjectID assigned to employee number 10: 1000, 1300

Exercise 3: Use Join Show all the project names that are assigned to Ken – Use join IST21018 Shared column: ProjectIDShared column: EmployeeNumber

More about Modifying Data Insert – Add a new row in a table Update – Update the data in a table that matches the specified criteria Delete – Delete the data in a table that matches the specified criteria

Changing Data Values: UPDATE To change the data values in an existing row (or set of rows) use the Update statement UPDATE EMPLOYEE SET Phone=' ' WHERE EmployeeNumber = 11; UPDATE PROJECT SET ProjectID = 2000 WHERE ProjectID = 1000 UPDATE DEPARTMENT SET DepartmentName = 'Public Relation' WHERE ProjectID = 'Marketing'

Deleting Data: DELETE To delete a row or set of rows from a table using the DELETE statement DELETE FROM EMPLOYEE WHERE EemplyeeNumber = 12; DELETE FROM EMPLOYEE WHERE EemplyeeNumber = 8;

Update/Delete Rules CASCADE – Affect the primary key as well as the foreign keys NO ACTION – Cannot be changed/deleted unless a record is NOT referred by any other table at all.