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.

Slides:



Advertisements
Similar presentations
WHERE Clause Chapter 2. Objectives Limit rows by using a WHERE clause Use the LIKE operator Effect of NULL values Use compound conditions Use the BETWEEN.
Advertisements

4d. Structured Query Language – JOIN Operation Lingma Acheson Department of Computer and Information Science IUPUI CSCI N207 Data Analysis with Spreadsheets.
SQL-week5-1 In-Class Exercise Answer IST 210 Organization of Data IST2101.
Introduction to Structured Query Language (SQL)
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 9 Structured Query Language.
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.
Introduction to Structured Query Language (SQL)
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.
Structured Query Language (SQL)
Concepts of Database Management Sixth Edition
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.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
 The WHERE clause, also called the predicate, provides the power to narrow down the scope of the data retrieved.  Comparison Operators Comparison OperatorDefinition.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
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.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
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.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
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,
Concepts of Database Management Seventh Edition
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.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
STRUCTURED QUERY LANGUAGE SQL-II IST 210 Organization of Data IST210 1.
4a. Structured Query Language - SELECT Statement Lingma Acheson Department of Computer and Information Science IUPUI CSCI N207 Data Analysis with Spreadsheets.
STRUCTURED QUERY LANGUAGE SQL-III IST 210 Organization of Data IST210 1.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
SQL-5 In-Class Exercise Answer IST 210 Organization of Data IST2101.
Structured Query Language
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.
Microsoft Access 2000 Presentation 5 Creating Databases Part IV (Creating Queries)
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.
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.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
IST 220 – Intro to DB Lab 2 Specifying Criteria in SELECT Statements.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Structured Query Language SQL-II IST 210 Organization of Data IST2101.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
IST 220 – Intro to DB Lab 2 Specifying Criteria in SELECT Statements.
CHAPTER 7 DATABASE ACCESS THROUGH WEB
Writing Basic SQL SELECT Statements
JDBC.
SQL.
Using the Set Operators
SQL Tutorial.
Structured Query Language
Restricting and Sorting Data
Introduction To Structured Query Language (SQL)
Lesson 3 Chapter 10.
Manipulating Data Lesson 3.
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

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 Table WHERECondition;

Running Example IST2104

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

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

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

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;

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 Department FROM EMPLOYEE; SELECT DISTINCT Department FROM EMPLOYEE;

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

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

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

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');

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');

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

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!

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 EMPLOYEE WHERE LastName LIKE 'J%'; Find all employees whose phone number ends with 10. SELECT * FROM EMPLOYEE WHERE 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 3 rd letter and ends with letter “r”