SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Download three SQL script files from wiki page.

Slides:



Advertisements
Similar presentations
SQL – Lesson II Grade 12.
Advertisements

SQL – Part 2 Multiple Tables CIS 324 – Chapter 5.
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.
4c. Structured Query Language - Built-in Functions Lingma Acheson Department of Computer and Information Science IUPUI CSCI N207 Data Analysis with Spreadsheets.
Database Programming Sections 5 & 6 – Group functions, COUNT, DISTINCT, NVL, GROUP BY, HAVING clauses, Subqueries.
LECTURE 10.  Group functions operate on sets of rows to give one result per group.
Structured Query Language Chapter Three Part 3 – Inserts, Updates, Deletes.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 9 Structured Query Language.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Structured Query Language Part I Chapter Three CIS 218.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Structured Query Language (SQL)
Mary K. Olson PS Reporting Instance – Query Tool 101.
Concepts of Database Management Sixth Edition
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
Microsoft Access 2010 Chapter 7 Using SQL.
PHP and SQL Server: Queries IST2101. Three-Tier Architecture Three-tier architecture means that the Web server and the DBMS are on separate servers IST2102.
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.
Computer Science 101 Web Access to Databases SQL – Extended Form.
Structured Query Language
PHP and SQL Server: Queries IST2101. Project Report 4 SQL Queries Due Sunday, 4/5 at 11:59pm Instructions on how to access team webspace and SQL database.
Enhancements to the GROUP BY Clause Fresher Learning Program January, 2012.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 5 th Edition.
15 Structured Query Language (SQL). 2 Objectives After completing this section, you should be able to: Understand Structured Query Language (SQL) and.
Chapter 3 Single-Table Queries
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
SQL – Structured Query Language CIS 324 – Chapter 5.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
Structured Query Language. Group Functions What are group functions ? Group Functions Group functions operate on sets of rows to give one result per group.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
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”
Information Resource Engineering SQL4. Recap - Ordering Output  Usually, the order of rows returned in a query result is undefined.  The ORDER BY clause.
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Select your database – Your database name is.
STRUCTURED QUERY LANGUAGE SQL-II IST 210 Organization of Data IST210 1.
STRUCTURED QUERY LANGUAGE SQL-III IST 210 Organization of Data IST210 1.
In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.
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.
1 Querying a Single Table Structured Query Language (SQL) - Part II.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
PHP and SQL Server: Queries IST2101. Steps to Design PHP Pages to Answer User Queries 1.Query generation – What SQL query do we need to retrieve the desired.
SQL-5 In-Class Exercise Answer IST 210 Organization of Data IST2101.
Structured Query Language
© Jalal Kawash Database Queries Peeking into Computer Science.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Aggregating Data Using Group Functions. What Are Group Functions? Group functions operate on sets of rows to give one result per group.
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Structured Query Language SQL-II IST 210 Organization of Data IST2101.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
SQL AGGREGATE FUNCTIONS
The Database Exercises Fall, 2009.
Working with Tables: Join, Functions and Grouping
Using SQL to Prepare Data for Analysis
Chapter 4 Summary Query.
Access: SQL Participation Project
Structured Query Language
Query Functions.
Section 4 - Sorting/Functions
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

SQL for Data Retrieval

Running Example IST2102

Data Preparation Login to SQL server using your account Download three SQL script files from wiki page and open them in SQL Server Management Studio. – SQL-Delete-Tables.sql – SQL-Create-Tables.sql – SQL-Insert-Data.sql Select your database – Your database name is your PSU ID – Alternatively, you can add one statement at the first line of each script: USE [your PSU ID]; Execute scripts.

Review of Previous Class: Queries SELECT is the best known SQL statement SELECT will retrieve information from the database that matches the specified criteria using the SELECT / FROM / WHERE framework SELECT ColumnName FROM Table WHERECondition;

Exercise 1: Review Questions Q1. Show the phone number of Accounting department (use Department table) – Use DEPARTMENT table – Use “WHERE”, slide #13 (week4-3.pptx) Q2. Show employees with FirstName as ‘Mary’ and LastName starting with letter ‘A’ – Use “OR”, slide #16 – Use wildcard, slide #21 IST2105

SQL for Data Retrieval: Sorting the Results Query results may be sorted using the ORDER BY clause SELECT * FROM EMPLOYEE ORDER BY LastName;

SQL for Data Retrieval: Sorting the Results By default, SQL sorts in ascending order. To sort in descending order, use: SELECT FirstName, LastName, Phone, Department FROM EMPLOYEE ORDER BY Department DESC;

SQL for Data Retrieval: Sorting the Results Query FirstName, LastName, Phone, Department from EMPLOYEE table, sort the result by Department in descending order, and then by LastName in ascending order IST2108 SELECT FirstName, LastName, Phone, Department FROM EMPLOYEE ORDER BY Department DESC, LastName ASC;

SQL for Data Retrieval: Built-in SQL Functions SQL provides several built-in functions – COUNT Counts the number of rows that match the specified criteria – MIN Finds the minimum value for a specific column for those rows matching the criteria – MAX Finds the maximum value for a specific column for those rows matching the criteria – SUM Calculates the sum for a specific column for those rows matching the criteria – AVG Calculates the numerical average of a specific column for those rows matching the criteria

SQL for Data Retrieval: Built-in Function Examples Count the total number of employees: SELECT COUNT(*) FROM EMPLOYEE; Find the minimum, maximum, and average hours for all projects. SELECT MIN(MaxHours) AS MinimumHours, MAX(MaxHours) AS MaximumHours, AVG(MaxHours) AS AverageHours FROM PROJECT

SQL for Data Retrieval: Built-in Function Examples SQL standard does not allow column names to be mixed with built-in functions, except in certain uses of GROUP BY clause (to be discussed next) SELECTMaxHours, SUM(MaxHours) FROMPROJECT WHEREProjectID <= 1200; Bad query. MaxHours is not an aggregate result, but SUM() is.

Exercise 2 Q1. Count how many projects with MaxHours less than 130 – Use “WHERE” Q2. Show average MaxHours of all projects Q3. (Challenging) Count how many projects with MaxHours less than the average MaxHours? IST21012

SQL for Data Retrieval: Providing Subtotals: GROUP BY Subtotals may be calculated by using the GROUP BY clause – Show how many employees in each department SELECT Department, COUNT(*) AS NumOfEmployees FROM EMPLOYEE GROUP BY Department The HAVING clause may be used to restrict which data is displayed – Only show those departments with more than one employee SELECT Department, COUNT(*) AS NumOfEmployees FROM EMPLOYEE GROUP BY Department HAVING COUNT(*) > 1;

Exercise 3 Q1. Group assignments by employees. Show employee number and total hours worked Q2. Add a constraint to Q1: only show the employees with total hours worked more than 100 Q3. Add a constraint to Q1: only show the employees with employee number less than 5 – Try to use two ways to answer this query: (1) Use “WHERE”, (2) Use “HAVING” IST21014

Retrieving Information from Multiple Tables Two approaches – Subqueries – Joins

Example 1: Querying Two Tables IST21016 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 IST21017 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 4: Use Subquery IST21018 Show the names of employees who is assigned to ProjectID 1000 – First write two separate queries and then merge into one query 18 Q2. 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 IST21019 SELECTFirstName, LastName 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);

Example 1: Use Join IST21020 SELECTFirstName, LastName 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 4: Use Join Show the names of employees who is assigned to ProjectID 1000 IST21021 Shared column: EmployeeNumber