Structured Query Language SQL-II IST 210 Organization of Data IST2101.

Slides:



Advertisements
Similar presentations
Database Queries and Structured Query Language (SQL) J.G. Zheng May 16 th 2008.
Advertisements

SQL-week5-1 In-Class Exercise Answer IST 210 Organization of Data IST2101.
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Introduction to Structured Query Language (SQL)
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Introduction to Structured Query Language (SQL)
© 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)
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
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.
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.
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 Operations Aggregate Functions Having Clause Database Access Layer A2 Teacher Up skilling LECTURE 5.
Structured Query Language
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Download three SQL script files from wiki page.
Ceng 356-Lab2. Objectives After completing this lesson, you should be able to do the following: Limit the rows that are retrieved by a query Sort the.
Introduction to SQL J.-S. Chou Assistant Professor.
Rationale Aspiring Database Developers should be able to efficiently query and maintain databases. This module will help students learn the Structured.
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.
Microsoft Access 2010 Chapter 7 Using SQL. Change the font or font size for SQL queries Create SQL queries Include fields in SQL queries Include simple.
Database Queries. Queries Queries are questions used to retrieve information from a database. Contain criteria to specify the records and fields to be.
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.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting MySQL – Selecting Data.
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
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”
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Select your database – Your database name is.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
STRUCTURED QUERY LANGUAGE SQL-II IST 210 Organization of Data IST210 1.
STRUCTURED QUERY LANGUAGE SQL-III IST 210 Organization of Data IST210 1.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
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.
Structured Query Language
SQL SELECT Getting Data from the Database. Basic Format SELECT, FROM WHERE (=, >, LIKE, IN) ORDER BY ; SELECT LastName, FirstName, Phone, City FROM Customer.
Session 9 Accessing Data from a Database. RDBMS and Data Management/ Session 9/2 of 34 Session Objectives Describe the SELECT statement, its syntax and.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
© 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.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
+ Structured Query Language Part 2 KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall.
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.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
CHAPTER 7 DATABASE ACCESS THROUGH WEB
Writing Basic SQL SELECT Statements
SQL AGGREGATE FUNCTIONS
JDBC.
SQL.
Using SQL to Prepare Data for Analysis
Chapter 4 Summary Query.
Access: SQL Participation Project
Structured Query Language
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

Structured Query Language SQL-II IST 210 Organization of Data IST2101

Running Example IST2102

Data Preparation Login Microsoft SQL Server 1.Start --> Application Development and Management --> Microsoft SQL Server > SQL Server 2014 Management Studio 2.Server Type: Database Engine 3.Server Name: upsql 4.Authentication: Windows Authentication 5.Hit “Connect” Prepare tables 1.Download SQL script files from course schedule page SQL-Delete-Tables.sql, SQL-Create-Tables.sql, SQL-Insert-Data.sql 2.Execute them one by one 1.SQL-Delete-Tables.sql 2.SQL-Create-Tables.sql 3.SQL-Insert-Data.sql IST2103

Check Your Database Click “New Query” Type Click “Execute” See whether it shows 5 rows in the results panel You can try other tables by replacing “PROJECT” with other table names IST2104 SELECT * FROM PROJECT;

View the Database Diagram Right click “Database Diagrams” Click “New Database Diagrams” Select all tables Right click “Database Diagrams” Click “New Database Diagrams” Select all tables IST2105

Content in today’s class Query: SELECT … FROM … WHERE Query a single table IST2106

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; IST2107

SQL for Data Retrieval: The Results of a Query is a Relation A query pulls information from one or more relations and creates (temporarily) a new relation This allows a query to: –Create a new relation –Feed information to another query (as a “sub- query”) IST2108

SQL for Data Retrieval: Displaying All Columns To show all of the column values for the rows that match the specified criteria, use an asterisk ( * ) SELECT * FROM PROJECT; IST2109

SQL for Data Retrieval: Displaying Specified Columns The following SQL statement queries three of the six columns of the PROJECT table: SELECT ProjectName, Department, MaxHours FROM PROJECT; IST21010

SQL for Data Retrieval: Showing Each Row Only Once The DISTINCT keyword may be added to the SELECT statement to inhibit duplicate rows from displaying SELECT Department FROM EMPLOYEE; SELECT DISTINCT Department FROM EMPLOYEE; IST21011

Exercise 1 Q1. Show the firstname and lastname of all employees Q2. Show all distinct firstname of employees IST21012

SQL for Data Retrieval: Specifying Search Criteria The WHERE clause stipulates the matching criteria for the record that are to be displayed IST21013 SELECT* FROMPROJECT WHEREDepartment = 'Finance'; SELECT* FROMPROJECT WHEREMaxHours > 135;

SQL for Data Retrieval: Match Criteria The WHERE clause match criteria may include –Equals “=“ –Not Equals “<>” –Greater than “>” –Less than “<“ –Greater than or Equal to “>=“ –Less than or Equal to “<=“ IST21014

SQL for Data Retrieval: Match Operators Multiple matching criteria may be specified using –AND Representing an intersection of the data sets –OR Representing a union of the data sets IST21015

SQL for Data Retrieval: Operator Examples SELECT* FROMPROJECT WHEREDepartment = 'Finance' AND MaxHours > 135; SELECT* FROMPROJECT WHEREDepartment = 'Finance' OR MaxHours > 135; IST21016

Exercise 2 Q1. Show all the employee with firstname as Mary or department is Accounting Q2. Show all the employee with department is Finance or Accounting or Marketing IST21017

SQL for Data Retrieval: A List of Values The WHERE clause may include the IN keyword to specify that a particular column value must be included in a list of values SELECT FirstName, LastName, Department FROM EMPLOYEE WHERE Department IN ('Finance', 'Accounting', 'Marketing'); IST21018

SQL for Data Retrieval: The Logical NOT Operator Any criteria statement may be preceded by a NOT operator which is to say that all information will be shown except that information matching the specified criteria SELECT FirstName, LastName, Department FROM EMPLOYEE WHERE Department NOT IN ('Accounting', 'Finance', 'Marketing'); IST21019

SQL for Data Retrieval: Allowing for Wildcard Searches The SQL LIKE keyword allow searches on partial data values LIKE can be paired with wildcards to find rows matching a string value –Multiple character wildcard character is a percent sign (%) –Single character wildcard character is an underscore (_) IST21020

SQL for Data Retrieval: Wildcard Search Examples Find all employees whose last name starts with “J”. SELECT* FROM EMPLOYEE WHERE LastName LIKE 'J%'; Find all employees whose last name starts with “J” and only has 5 letters in total. SELECT * FROM EMPLOYEE WHERELastName LIKE 'J____'; IST21021

Exercise 3 Q1. Find employees that have first name ends with “y” and has 4 letters in total Q2. Find employees that have first name starts with letter “R” and ends with letter “d” Q3. Find employees that have first name with the 3 rd letter is “a” and ends with letter “r” IST21022

SQL for Data Retrieval: IS NULL Keyword Find all rows which have null values for an specified attribute: SELECT FirstName, LastName, Phone, Department FROMEMPLOYEE WHEREPhone IS NULL; IST21023 SELECTFirstName, LastName, Phone, Department FROMEMPLOYEE WHEREPhone = NULL; Bad query

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

SQL for Data Retrieval: Sorting the Results By default, SQL sorts in ascending order. To sort in descending order, use: IST21025 SELECT * FROM EMPLOYEE ORDER BY LastName Desc;

SQL for Data Retrieval: Sorting the Results Sort the result by LastName in descending order, and then by FirstName in ascending order IST21026 SELECT * FROM EMPLOYEE ORDER BY LastName DESC, FirstName 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 IST21027

SQL for Data Retrieval: Built-in Function Examples Count the total number of projects: SELECT COUNT(*) FROM PROJECT; 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 IST21028

Exercise 4 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? IST21029

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 Only show those departments with more than one employee The HAVING clause may be used to restrict which data is displayed HAVING COUNT(*) > 1; IST21030

Exercise 5 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 IST21031