Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 SQL for Data Retrieval

2 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

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

4 Running Example IST2104

5 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.sql 2.SQL-Create-Tables.sql 3.SQL-Insert-Data.sql Execute scripts (1  2  3).

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

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

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

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

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;

12 Exercise 1 Q1. Show all the FirstName and LastName of 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 SELECT FirstName, LastName FROM EMPLOYEE WHERE Department = 'Administration‘ 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 “<=“

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

16 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 EmployeeNumber FROM ASSIGNMENT WHERE HoursWorked < 20 OR HoursWorked > 50; Find the projects from Finance department with MaxHours larger than 135. SELECT* FROMPROJECT WHEREDepartment = 'Finance' AND MaxHours > 135;

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 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 LastName FROM EMPLOYEE WHERE Department IN ('Finance', 'Legal', 'Info Systems');

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 LastName FROM EMPLOYEE WHERE Department NOT IN ('Finance', 'Legal', 'Info Systems');

20 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 EmployeeNumber FROM ASSIGNMENT WHERE HoursWorked BETWEEN 20 and 50

21 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; SELECTFirstName, LastName, Phone, Department FROMEMPLOYEE WHEREPhone = NULL; Bad Query!

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

23 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 phone number ends with 10. SELECT * FROM EMPLOYEE WHERE Phone LIKE '___-___-__10';

24 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 3 rd letter and ends with letter “r”


Download ppt "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."

Similar presentations


Ads by Google