Structured Query Language

Slides:



Advertisements
Similar presentations
SQL-week5-1 In-Class Exercise Answer IST 210 Organization of Data IST2101.
Advertisements

SQL Subqueries Objectives of the Lecture : To consider the general nature of subqueries. To consider simple versus correlated subqueries. To consider the.
Structured Query Language Chapter Three Part 3 – Inserts, Updates, Deletes.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
© 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)
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.
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.
Concepts of Database Management, Fifth Edition
SQL 資料庫查詢語言 取材自 EIS, 3 rd edition By Dunn et al..
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 5 th Edition.
Chapter 3 Single-Table Queries
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
HAP 709 – Healthcare Databases SQL Data Manipulation Language (DML) Updated Fall, 2009.
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.
Database Basics CPSC 4670/ Purpose of a Database The purpose of a database is to keep track of things Unlike a list or spreadsheet, a database.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
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 TRANSACTION. Transaction It is a logical unit of work that must succeed or fail in its entirety. A transaction is an atomic operation which may.
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
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.
Intro to SQL Management Studio. Please Be Sure!! Make sure that your access is read only. If it isn’t, you have the potential to change data within your.
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.
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.
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.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Structured Query Language
+ Midterm Review. + Notes from the Midterm When updating, deleting records Make sure you have a WHERE statement that only will give you the row(s) you.
© 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.
+ 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.
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.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Structured Query Language SQL-II IST 210 Organization of Data IST2101.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
IFS180 Intro. to Data Management Chapter 10 - Unions.
CHAPTER 7 DATABASE ACCESS THROUGH WEB
Structured Query Language
SQL Query Getting to the data ……..
MySQL Subquery Source: Dev.MySql.com
SQL AGGREGATE FUNCTIONS
PL/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-1
IST 210: Organization of Data
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Chapter 4 Summary Query.
Access: SQL Participation Project
M1G Introduction to Database Development
Contents Preface I Introduction Lesson Objectives I-2
Query Functions.
Manipulating Data Lesson 3.
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

Structured Query Language Chapter Three Structured Query Language Part 2 – Queries

Sample DB details PROJECT (ProjectID, Name, Department, MaxHours) EMPLOYEE (EmployeeNumber, Name, Phone, Department) ASSIGNMENT (ProjectID, EmployeeNum, HoursWorked)

Referential Integrity Constraints for Sample DB ProjectID in ASSIGNMENT must exist in ProjectID in PROJECT EmployeeNum in ASSIGNMENT must exist in EmployeeNumber in EMPLOYEE

Business Rules for Sample DB If a PROJECT row is deleted, then all of the ASSIGNMENT rows that are connected to the delete PROJECT row also will be deleted. If an EMPLOYEE row is deleted and that row is connected to any ASSIGNMENT, the EMPLOYEE row deletion will be disallowed. (It must first be re-assigned to another employee).

SQL for Data Retrieval (Queries) SELECT is the best known SQL statement SELECT will retrieve information from the database that matches the specified criteria SELECT EmpName FROM Emp;

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 for a query to: Create a new relation Feed information to another query (as a “sub-query”)

Displaying All Columns: * To show all of the column values for the rows that match the specified criteria, use an * SELECT * FROM Emp;

Showing a Row Only Once: DISTINCT A qualifier may be added to the SELECT statement to inhibit duplicate rows from displaying SELECT DISTINCT DeptID FROM Emp;

Specifying Search Criteria: WHERE The WHERE clause stipulates the matching criteria for the record that are to be displayed SELECT EmpName FROM Emp WHERE DeptID = 15;

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 “<=“

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

Operator Examples SELECT EmpName FROM Emp WHERE DeptID < 7 OR DeptID > 12;

Operator Examples SELECT EmpName FROM Emp WHERE DeptID = 9 AND SalaryCode <= 23;

A List of Values The WHERE clause may specify that a particular column value must be included in a list of values SELECT EmpName FROM Emp WHERE DeptID IN (4, 8, 9);

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 EmpName FROM Emp WHERE DeptID NOT IN (4, 8, 9);

Finding Data Matching a Range of Values: BETWEEN SQL provides a BETWEEN statement that allows a user to specify a minimum and maximum value on one line SELECT EmpName FROM Emp WHERE SalaryCode BETWEEN 10 AND 45;

Allowing for Wildcard Searches: LIKE Sometimes it may be advantageous to find rows matching a string value using wildcards Single character wildcard character is an underscore (_) Multiple character wildcard character is a percent sign (%)

Wildcard Search Examples SELECT EmpID FROM Emp WHERE EmpName LIKE ‘Kr%’; SELECT EmpID WHERE Phone LIKE ‘616-___-____’;

Sorting the Results: ORDER BY The results may be sorted using the ORDER BY clause SELECT * FROM Emp ORDER BY EmpName;

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

Built-in SQL Functions (continued) 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

Built-in Function Examples SELECT COUNT(DISTINCT DeptID) FROM Emp; SELECT MIN(Hours), MAX(Hours), AVG(Hours) FROM Project WHERE ProjID > 7;

Providing Subtotals: GROUP BY Subtotals may be calculated by using the GROUP BY clause SELECT DeptID, COUNT(*) FROM Emp GROUP BY DeptID HAVING Count(*) > 3;

Retrieving Information from Multiple Tables SubQueries As stated earlier, the result of a query is a relation. As a result, a query may feed another query. This is called a subquery Joins Another way of combining data is by using a Join Join [also called an Inner Join] Left Outer Join Right Outer Join

Subquery SELECT EmpName FROM Emp WHERE DeptID in (SELECT DeptID FROM Department WHERE DeptName LIKE ‘Accounts%’);

Join SELECT EmpName FROM Emp, Department WHERE Emp.DeptID = Department.DeptID AND Department.DeptName LIKE ‘Account%’;

Left Outer Join Left Outer Join SELECT EmpName FROM Emp LEFT JOIN Department WHERE Emp.DeptID = Department.DeptID AND Department.DeptName LIKE ‘Account%’;

Right Outer Join Right Outer Join SELECT EmpName FROM Department RIGHT JOIN Emp WHERE Emp.DeptID = Department.DeptID AND Department.DeptName LIKE ‘Account%’;