Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

Slides:



Advertisements
Similar presentations
Structured Query Language (SQL)
Advertisements

Database Queries and Structured Query Language (SQL) J.G. Zheng May 16 th 2008.
IT203 Unit 9: Database Security II Is It Secure? Copyright © 2012 Pearson Education, Inc. Publishing as Prentice HallChapter8.1.
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
SQL: The Query Language Part 2
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
INSERT BOOK COVER 1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Excel 2010 by Robert Grauer, Keith.
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
Yong Choi School of Business CSU, Bakersfield
With Microsoft Access 2010© 2011 Pearson Education, Inc. Publishing as Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Access.
Copyright © 2014 Pearson Education, Inc. Chapter 7 SQL Chapter7.1.
With Microsoft Access 2010 © 2011 Pearson Education, Inc. Publishing as Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Access.
INSERT BOOK COVER 1Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Access 2010 by Robert Grauer, Keith Mast,
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Introduction to Structured Query Language (SQL)
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.
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.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Concepts of Database Management Sixth Edition
Microsoft Access 2010 Chapter 7 Using SQL.
Structured Query Language
INFORMATION TECHNOLOGY IN BUSINESS AND SOCIETY SESSION 16 – SQL SEAN J. TAYLOR.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 5 th Edition.
LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
INSERT BOOK COVER 1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Access 2010 by Robert Grauer, Keith.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
Using Special Operators (LIKE and IN)
1 Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall. Access Module 2 Workshop 3 Queries and Data Access Series Editor Amy Kinser by.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Copyright © 2010 Pearson Education, Inc. Publishing as Prentice Hall 11 Committed to Shaping the Next Generation of IT Experts. Chapter 3: Customize, Analyze,
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
By Mary Anne Poatsy, Keith Mulbery, Eric Cameron, Jason Davidson, Rebecca Lawson, Linda Lau, Jerri Williams Chapter 6 Action and Specialized Queries 1.
SQL SELECT Getting Data from the Database. Basic Format SELECT, FROM WHERE (=, >, LIKE, IN) ORDER BY ; SELECT LastName, FirstName, Phone, City FROM Customer.
1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Access 2010 by Robert Grauer, Keith Mast, and Mary Anne.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
CHAPTER 7 DATABASE ACCESS THROUGH WEB
SQL Query Getting to the data ……..
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Introduction To Structured Query Language (SQL)
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Database systems Lecture 3 – SQL + CRUD
Structured Query Language – The Fundamentals
Introduction To Structured Query Language (SQL)
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall IT203 Unit 6: SQL Queries I SQL Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall SQL Overview SQL is the language of relational databases. It is used for every aspect of database development and management. Anyone who works with relational databases is expected to have knowledge of SQL. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall History SQL is the programming language used for accessing and manipulating data and objects in relational databases. The first versions of SQL were developed by IBM in the 1970s. SQL first became an ANSI standard in 1986 and an ISO standard in 1987. There was a major revision to the standard in 1992. Additional modifications were made in 1999, 2003, and 2006. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Nature of SQL SQL is a declarative language. Procedural languages like C# or Java describe how to accomplish a task step by step. In a declarative language you say what you want to do, not how. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall SQL Functionality SQL is not case sensitive. In some environments, SQL statements must end with a semicolon. SQL is usually divided into two broad areas of functionality: DDL (Data Definition Language) DML (Data Manipulation Language) Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall DDL Data Definition language is the set of SQL keywords and commands used to create, alter and remove database objects. A example is the CREATE TABLE command: CREATE TABLE TestTable ( TestID INT IDENTITY (1,1), TestDescription NVARCHAR(255) ) Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall DML Data Manipulation Language is the set of keywords and commands used to retrieve and modify data. SELECT, UPDATE, INSERT, and DELETE are the primary actions of DML. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

Starting a New Query Window One way to start a new query window in SQL Server is to right-click the database folder in the object explorer window and select New Query from the context menu. It will open up a new query window. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Select Statement The SELECT statement is used to retrieve data from the database. The basic syntax is: SELECT <columnName>, <columnName> FROM <TableName> SELECT StudentFirstName, StudentLastName, StudentPhone FROM Student Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall The * WildCard Instead of listing each of the columns, you can use an * to include all columns. SELECT * FROM Tutor Listing the columns does give you the ability to choose both which columns and which order to present them. With the *, you return all the columns in the order they have in the underlying table. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Distinct Key Word Sometimes a query will return multiple duplicate values. For instance, the statement SELECT TutorKey FROM Session could return numerous instances of each customer. The DISTINCT keyword will make it so it only returns one instance of each TutorKey. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Distinct Continued SELECT DISTINCT TutorKey FROM Session The DISTINCT keyword always operates on the whole row, not on individual columns. It only returns distinct rows. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Calculations You can do calculations in SELECT statements. SELECT ItemNumber, ItemPrice, Quantity, ItemPrice * Quantity FROM CustomerOrder Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Operators Operator Description * Multiplication / Division + Addition - Subtraction % Modulus (returns the remainder in integer division) Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Order of Operations The order of operation is the same as in algebra. Whatever is in parenthesis is executed first. If parentheses are nested, the innermost is executed first, then the next most inner, etc. Then all division and multiplication left to right. And finally all addition and subtractions left to right. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall SORTING You can sort the results of a query by using the keywords ORDER BY. SELECT * FROM Session ORDER BY SessionDate ORDER BY does an ascending A-Z, 1-10 etc. sort by default. You can change the direction by using the DESC keyword after the field to be sorted. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Aliasing Sometimes it is useful to alias a column name to make a more readable result set. SELECT StudentLastName AS [Last Name], StudentFirstName AS [First Name] FROM Student The AS keyword is optional. Double quotes “ “ can be used instead of square brackets. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Where Clause The WHERE clause allows you to limit the rows you return in a query. You use the WHERE clause to specify the criteria by which the rows will be filtered. SELECT LastName, FirstName, Phone, City FROM Customer WHERE City = 'Seattle' Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Other Criteria As well as equal, you can use other operators for the criteria: > < >= =< Character and date values in the criteria are quoted with single quotes. Numerical values are not quoted. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Like The LIKE keyword used in a WHERE operator with a wildcard (% or _) allows you to search for patterns in character based fields. The following returns all items whose name starts with "T". SELECT ItemName, ItemPrice FROM Inventory WHERE ItemName LIKE 'T%' Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Between The BETWEEN keyword can be used in criteria to return values between two other values. BETWEEN is inclusive of its ends. SELECT TutorKey, SessionDate, StudentKey FROM Session WHERE SessionDate BETWEEN '11/1/2008' AND '11/30/2008' Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall AND, OR, NOT You can use keywords AND, OR, and NOT to combine criteria in a query. AND is exclusive. Or is inclusive. WHERE City='Seattle' OR City='Portland' returns all records that have either Seattle or Portland for their city. WHERE City='Seattle' AND City='Portland' returns nothing because the record cannot have both at the same time. NOT excludes. WHERE NOT City='Portland' returns every city except Portland. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Null Nulls are special cases. They are not a value and so cannot be compared to a value using = or < or >. To locate nulls, you can use the IS keyword in a criteria: WHERE StudentKey IS NULL WHERE StudentKey IS NOT NULL Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Functions Functions always have the same basic syntax: <function name>(function arguments) There are hundreds of built-in functions. We will be concerned with two broad types of functions: Scalar functions Aggregate functions Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Scalar Functions Scalar functions operate on a single row at a time. Here is a list of scalar functions used in this chapter: Function Name Description GETDATE() Returns current date and time MONTH Returns the month as in integer (1 to 12) from a Date value YEAR Returns the Year as a four-digit integer from a date value Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall Aggregate Functions Aggregate functions operate on multiple rows at a time. Here is a table of common aggregate functions: Aggregate Function Description COUNT Counts the number of values : COUNT(*) counts all the rows. COUNT(columnName) counts all the values in the column but ignores nulls SUM Sums or totals numeric values: SUM (InStock) AVG Returns the mean average of a set of numeric values: AVG(Price). By default nulls are ignored. MAX Returns the highest value in a set of numeric or datetime values: MAX(price) MIN Returns the smallest value in a set of numeric or datetime value: MIN(Price) Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

Using DISTINCT in Aggregate Functions You can use the DISTINCT keyword with aggregate functions. Doing so means the function will ignore duplicate values in its calculation. SELECT COUNT(DISTINCT studentKey) AS [Unduplicated] FROM Session Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. Printed in the United States of America. Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall