SQL-week5-1 In-Class Exercise Answer IST 210 Organization of Data IST2101.

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.
4c. Structured Query Language - Built-in Functions Lingma Acheson Department of Computer and Information Science IUPUI CSCI N207 Data Analysis with Spreadsheets.
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.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
SQL SQL stands for Structured Query Language SQL allows you to access a database SQL is an ANSI standard computer language SQL can execute queries against.
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.
Database I. Practice 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.
Data Modeling and Entity- Relationship Model I IST2101.
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.
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Download three SQL script files from wiki page.
 Continue queries ◦ You completed two tutorials with step-by-step instructions for creating queries in MS Access. ◦ Now must apply knowledge and skills.
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.
1 Agenda – 03/25/2014 Login to SQL Server 2012 Management Studio. Answer questions about HW#7 – display answers. Exam is 4/1/2014. It will be in the lab.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-column subqueries, Multiple-row Subqueries, Correlated Subqueries 11/2/10,
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Nested Queries (Sub-Queries). Objectives Learn how to run a query as a nested sub-query Condition on nested query Application of nested query Restriction.
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.
1 Agenda – 10/24/2013 Answer questions from lab on 10/22. Present SQL View database object. Present SQL UNION statement.
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”
Getting to Know SQL. © Jim Hope 2002 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement TRANSFORM.
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.
CREATE TABLE CREATE TABLE statement is used for creating relations Each column is described with three parts: column name, data type, and optional constraints.
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.
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.
Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.
Structured Query Language
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
The Student Registry Database Ian Van Houdt and Muna alfahad.
Lecture Access – Queries. What’s a Query? A question you ask a database –ie: “Who are my Stockton customers?” –ie: “How much did Bob sell on the 14th?”
Drill Consider the following tables with the following fields: Student: FName, LName, StudentID, Age, Yr, Course Grades: ID, P1, P2, P3 1.Display the.
SQL Aggregeringsfunktioner. AGGREGATE FUNCTIONS Include COUNT, SUM, MAX, MIN, and AVG Query 15: Find the maximum salary, the minimum salary, and the average.
MIDTERM REVIEW IST 210 Organization of Data IST210 1.
Advanced Accounting Information Systems Day 12 Understanding the SQL Language September 21, 2009.
Structured Query Language SQL-II IST 210 Organization of Data IST2101.
Structured Query Language SQL-I IST 210 Organization of Data IST2101.
Originating the Required 2nd CPI Position Using Electronic Personnel Action Forms (EPAFs)
BBY 464 Semantic Information Management (Spring 2016) Semantic Query Languages Yaşar Tonta & Orçun Madran [yasartonta, Hacettepe.
Structured Query Language
ISQS 3358, Business Intelligence Database vs. Data Warehouse
SQL AGGREGATE FUNCTIONS
3d. Structured Query Language – JOIN Operations
ISQS 6339, Business Intelligence Database vs. Data Warehouse
Field Table Sort Show Criteria OR
MIS 562 Enthusiastic Studysnaptutorial.com
Chapter 2 Views.
Structured Query Language
Chapter 2 Views.
Using CASE Value expression
SQL Views Appendix C DATABASE CONCEPTS, 3rd Edition
M1G Introduction to Database Development
Query Functions.
BUS2206 Access Lab Queries Second Term,
Presentation transcript:

SQL-week5-1 In-Class Exercise Answer IST 210 Organization of Data IST2101

Exercise 1: Review Questions Q1. Show the phone number of Accounting department (use Department table) – Use DEPARTMENT table – Use “WHERE”, slide #12 Q2. Show employees with FirstName as ‘Mary’ and LastName starting with letter ‘A’ – Use “OR”, slide #15 – Use wildcard, slide #20 IST2102

3 /* Q1. Show the phone number of Accounting department */ SELECT Phone FROM DEPARTMENT WHERE DepartmentName = 'Accounting'; /* Q2. Show employees with firstname as ‘Mary’ and lastname starting with letter ‘A’ */ SELECT * FROM EMPLOYEE WHERE FirstName = 'Mary' and LastName LIKE 'A%';

Exercise 2 Q1. Count how many projects with MaxHours less than 130 Q2. Show average MaxHours of all projects (Challenging question) Can you use one SQL query to answer: – Count how many projects with MaxHours less than average MaxHours? IST2104

5 /*** Count how many projects with MaxHours less than 130 ***/ SELECT COUNT(*) FROM PROJECT WHEREMaxHours < 130; /*** Show average MaxHours of all projects ***/ SELECTAVG(MaxHours) FROMPROJECT; /*** Count how many projects with MaxHours less than average MaxHours. ***/ SELECTCOUNT(*) FROMPROJECT WHEREMaxHours < (SELECT AVG(MaxHours) FROM PROJECT);

Exercise 3 Q1. Group assignments by employees. Show employee numbers and total hours work Q2. Add a constraint to Q1: 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 IST2106

7 /*** Group assignments by employees. Show employee numbers and total hours worked. ***/ SELECT EmployeeNumber, SUM(HoursWorked) FROM ASSIGNMENT GROUP BY EmployeeNumber; /*** Group assignments by employees. Show employee numbers and total hours worked for those employees with hours more than 100. ***/ SELECT EmployeeNumber, SUM(HoursWorked) FROM ASSIGNMENT GROUP BY EmployeeNumber HAVING SUM(HoursWorked) > 100;

IST2108 /*** Add a constraint to Q1: only show the employees with number less than 5. ***/ SELECT EmployeeNumber, SUM(HoursWorked) FROM ASSIGNMENT GROUP BY EmployeeNumber HAVING EmployeeNumber < 5; SELECT EmployeeNumber, SUM(HoursWorked) FROM ASSIGNMENT WHERE EmployeeNumber < 5 GROUP BY EmployeeNumber;

Exercise 4 IST2109 The names of employees who is assigned to ProjectID 1000 – First write two separate queries and then merge into one query 9 Q2. Show the names of employees with the employee numbers from Q1 Q1. Find employee numbers who is assigned to projectID 1000 EMPLOYEE ASSIGNMENT

10 /*** Exercise 4: Show the names of employees who is assigned to ProjectID 1000 ***/ /* Q1. Find employee numbers who is assigned to projectID 1000 */ SELECT EmployeeNumber FROM ASSIGNMENT WHERE ProjectID = 1000; /* Q2. Show the names of employees with the employee numbers from Q1 */ SELECT FirstName, Lastname FROM EMPLOYEE WHERE EmployeeNumber IN (1,8,10); /* Use subquery to answer exercise 1 */ SELECT FirstName, Lastname FROM EMPLOYEE WHERE EmployeeNumber IN (SELECT EmployeeNumber FROM ASSIGNMENT WHERE ProjectID = 1000);

Exercise 4: use join Show the names of employees who is assigned to ProjectID 1000 IST21011 Shared column: EmployeeNumber

12 /* Use join to answer exercise 4 */ SELECT FirstName, Lastname FROM EMPLOYEE AS E, ASSIGNMENT AS A WHERE E.EmployeeNumber = A.EmployeeNumber AND A.ProjectID=1000;