Download presentation
Presentation is loading. Please wait.
Published byLora King Modified over 8 years ago
1
Structured Query Language SQL-II IST 210 Organization of Data IST2101
2
Running Example IST2102
3
Data Preparation Login Microsoft SQL Server 1.Start --> Application Development and Management --> Microsoft SQL Server 2014 --> 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
4
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;
5
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
6
Content in today’s class Query: SELECT … FROM … WHERE Query a single table IST2106
7
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
8
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
9
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
10
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
11
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
12
Exercise 1 Q1. Show the firstname and lastname of all employees Q2. Show all distinct firstname of employees IST21012
13
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;
14
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
15
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
16
SQL for Data Retrieval: Operator Examples SELECT* FROMPROJECT WHEREDepartment = 'Finance' AND MaxHours > 135; SELECT* FROMPROJECT WHEREDepartment = 'Finance' OR MaxHours > 135; IST21016
17
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
18
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
19
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
20
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
21
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
22
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
23
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
24
SQL for Data Retrieval: Sorting the Results Query results may be sorted using the ORDER BY clause SELECT * FROM EMPLOYEE ORDER BY LastName; IST21024
25
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;
26
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;
27
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
28
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
29
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
30
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
31
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.