24
SQL for Data Retrieval

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

Embed Size (px)

Citation preview

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 in your local computer (e.g., Desktop)

• For assignment 3, you should submit a single .sql file (not word or pdf file!) to ANGEL which contains all your scripts

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 TableWHERE Condition;

IST210 4

Running Example

Data Preparation

• Login to SQL server using your account• 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];

• Download three SQL script files from course schedule page and open them in SQL Server Management Studio.1. SQL-Delete-Tables.sql2. SQL-Create-Tables.sql3. SQL-Insert-Data.sql

• Execute scripts (1 2 3).

View the Database Diagram

• Right click “Database Diagrams”• Click “New Database Diagrams”• Select all tables

IST210 7

Diagram View

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”)

SQL for Data Retrieval:Displaying Specified Columns

• The following SQL statement queries three of the six columns of the PROJECT table:

SELECT ProjectName, Department, MaxHoursFROM PROJECT;

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;

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 DepartmentFROM EMPLOYEE;

SELECT DISTINCT DepartmentFROM EMPLOYEE;

IST210 12

Exercise 1

• Q1. Show all the FirstName and LastName of employees

• Q2. Show all distinct Firstname of employees

SQL for Data Retrieval:Specifying Search Criteria

• The WHERE clause stipulates the matching criteria for the record that are to be displayed

SELECT FirstName, LastNameFROM EMPLOYEEWHERE Department = 'Administration‘

SELECT *FROM PROJECTWHERE MaxHours > 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 “<=“

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

SQL for Data Retrieval:Operator Examples

• Find the employee numbers of those employees who worked less than 20 hours or more than 50 hours on their assignments.

SELECT EmployeeNumberFROM ASSIGNMENTWHERE HoursWorked < 20

OR HoursWorked > 50;

• Find the projects from Finance department with MaxHours larger than 135.

SELECT *FROM PROJECTWHERE Department = 'Finance'

AND MaxHours > 135;

IST210 17

Exercise 2

• Q1. Show all the employees with FirstName as George or Department is Finance

• Q2. Show all the employee with Department is Finance or Accounting or Marketing

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 LastNameFROM EMPLOYEEWHERE Department IN ('Finance', 'Legal',

'Info Systems');

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 LastNameFROM EMPLOYEEWHERE Department NOT IN ('Finance',

'Legal', 'Info Systems');

SQL for Data Retrieval:Finding Data in a Range of Values

• SQL provides a BETWEEN statement that allows a user to specify a minimum and maximum value on one line

SELECT EmployeeNumberFROM ASSIGNMENTWHERE HoursWorked BETWEEN 20 and 50

SQL for Data Retrieval:IS NULL Keyword

• Find all rows which have null values for an specified attribute:

SELECT FirstName, LastName, Phone, DepartmentFROM EMPLOYEEWHERE Phone IS NULL;

SELECT FirstName, LastName, Phone, DepartmentFROM EMPLOYEEWHERE Phone = NULL;

Bad Query!

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 (_)

SQL for Data Retrieval:Wildcard Search Examples

• Find all employees whose last name starts with “J”.

SELECT *FROM EMPLOYEEWHERE LastName LIKE 'J%';

• Find all employees whose phone number ends with 10.

SELECT *FROM EMPLOYEEWHERE Phone LIKE '___-___-__10';

Exercise 3

• Q1. Find employees whose FirstName ends with “y” and has 4 letters in total

• Q2. Find employees whose FirstName starts with letter “R” and ends with letter “d”

• Q3. Find employees whose FirstName has “a” as the 3rd letter and ends with letter “r”