1 SELECT statement. 2 Sample database Supplier Part supplies (0,n) colour s# snamep# pname amount citydob price qualitydate.

Slides:



Advertisements
Similar presentations
Relational Database Systems Higher Information Systems Advanced Implementation in MySQL/PHP.
Advertisements

Sorting Rows. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Construct a query to sort a results set in ascending.
Relational Database Systems Higher Information Systems Advanced Implementation in Filemaker Pro.
Database Languages Chapter 7. The Relational Algebra.
Copyright © 2004 Pearson Education, Inc.. Chapter 15 Algorithms for Query Processing and Optimization.
Information Resources Management February 27, 2001.
N-dimensional space navigation is only possible in a logical rather han a physical world. They didn’t have any books to look after - their information.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 SQL: Data Definition, Constraints, and Basic Queries and Updates.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Query by example (based on notes by Silberchatz,Korth, and Sudarshan.
Copyright © 2004 Pearson Education, Inc.. Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries.
Chapter 7: SQL, the Structured Query Language Soid Quintero & Ervi Bongso CS157B.
Computer Science 101 Web Access to Databases SQL – Extended Form.
Sorting data and Other selection Techniques Ordering data results Allows us to view our data in a more meaningful way. Rather than just a list of raw.
SELECT Advanced. Sorting data in a table The ORDER BY clause is used for sorting the data in either ascending or descending order depending on the condition.
Slide 1 Chapter 2 Introduction to Structured Query Language (SQL) ‏
With Microsoft Office 2007 Introductory© 2008 Pearson Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Office 2007 Introductory.
CS&E 1111 AcQueries Querying in Access Sorting data Aggregating Data Performing Calculations Objectives: Learn how to use the Access Query Design Tool.
Data Manipulation 11 After this lecture, you should be able to:  Understand the differences between SQL (Structured Query Language) and other programming.
1 Session 3 Welcome: To session 3-the 8 th. learning sequence “Relational algebra “ Recap : In the previous learning sequence, we discussed some example.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
SQL – Structured Query Language CIS 324 – Chapter 5.
1 Structured Query Language (SQL) CIS*2450 Advanced Programming Concepts.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
1 Data Manipulation in SQL  Department of Computer Science Northern Illinois University February 2001.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE)
Data Definition After this lecture, you should be able to:
1 From E-R Model to Relational Model. 2 Entity type sid#name address house#street pcode Student.
Data Manipulation 21 After this lecture, you should be able to:  Use SQL SELECT statement effectively to retrieve the data from multiple related tables.
INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
1 SQL SQL (Structured Query Language) : is a database language that is used to create, modify and update database design and data. Good Example of DBMS’s.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
IS 380 Introduction to SQL This lectures covers material from: database textbook chapter 3 Oracle chapter: 3,14,17.
INTRODUCTION TO SQL Chapter SELECT * FROM teacher WHERE INSTR (subject_id, ‘&1’)= 4 AND LOWER (subject_id) LIKE ‘HST%’ ; When prompted for the.
1 Access control. 2 GRANT statement 3 Access control GRANT statement GRANT statement may be used by a user to authorise various kinds of access to his/her.
Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2.
An Introduction to SQL For CS Overview of SQL  It is the standard language for relational systems, although imperfect  Supports data definition.
Chapter 11: Car Task Extension: Queries Importing CSV: Make sure currency is in Euros with two decimal places Using a suitable database package, import.
SQL SELECT Getting Data from the Database. Basic Format SELECT, FROM WHERE (=, >, LIKE, IN) ORDER BY ; SELECT LastName, FirstName, Phone, City FROM Customer.
1 Announcements Reading for next week: Chapter 4 Your first homework will be assigned as soon as your database accounts have been set up.  Expect an .
Drill Consider the following tables with the following fields: Student: FName, LName, StudentID, Age, Yr, Course Grades: ID, P1, P2, P3 1.Display the.
1 SQL II CIS*2450 Advanced Programming Concepts. 2 Data Types INTEGER –numbers without a decimal point –range is to SMALLINT –like.
SQL: 1 SQL Correspondence with Relational Algebra select A from r where B = 1 Assume r(AB) and s(BC). select B from r except select B from s select A as.
Sorting data and Other selection Techniques Ordering data results Allows us to view our data in a more meaningful way. Rather than just a list of raw.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
SQL Exercises. 1) Names of suppliers. Suppliers(sid, sname, address) Parts(pid, pname, color) Catalog(sid, pid, cost)
Structured Query Language IV Asma Ahmad (C. J. Date) Database Systems.
SQL, the Structured Query Language
STRUCTURE OF PRESENTATION :
Chapter 3 Introduction to SQL
Writing Basic SQL SELECT Statements
Chapter 3 Introduction to SQL(3)
Calculated Fields Exercises Queries
Retrieval Queries in SQL(DML)
Φροντιστήριο SQL (από το βιβλίο του Date)
Chapter 4 Summary Query.
CS4222 Principles of Database System
Session 3 Welcome: To session 3-the 8th. learning sequence
Section 4 - Sorting/Functions
STRUCTURE OF PRESENTATION :
SQL: Structured Query Language
Question 1: Basic Concepts (45 %)
‘ORDER BY’ Order by clause allows sorting of query results by one or more columns. Sorting can be done in ascending or descending. Default order is ascending.
STRUCTURE OF PRESENTATION :
Presentation transcript:

1 SELECT statement

2 Sample database Supplier Part supplies (0,n) colour s# snamep# pname amount citydob price qualitydate

3 SELECT statement Sample database Supplier( s#, sname, city, dob ) Sample database Supplier( s#, sname, city, dob ) primary key

4 SELECT statement Sample database Supplier( s#, sname, city, dob ) Part( p#, pname, colour, weight, quality ) Sample database Supplier( s#, sname, city, dob ) Part( p#, pname, colour, weight, quality ) primary key

5 SELECT statement Sample database Supplier( s#, sname, city, dob ) SP( s#, p#, sdate, amount ) Part( p#, pname, colour, weight, quality ) Sample database Supplier( s#, sname, city, dob ) SP( s#, p#, sdate, amount ) Part( p#, pname, colour, weight, quality ) primary key

6 SELECT statement Sample database Supplier( s#, sname, city, dob ) SP( s#, p#, sdate, amount ) Part( p#, pname, colour, weight, quality ) Sample database Supplier( s#, sname, city, dob ) SP( s#, p#, sdate, amount ) Part( p#, pname, colour, weight, quality ) primary key foreign key

7 SELECT statement Sample database Supplier( s#, sname, city, dob ) SP( s#, p#, sdate, amount ) Part( p#, pname, colour, weight, quality ) Sample database Supplier( s#, sname, city, dob ) SP( s#, p#, sdate, amount ) Part( p#, pname, colour, weight, quality )

8 SELECT statement SELECT statement template

9 SELECT statement SELECT statement template SELECT SELECT statement template SELECT

10 SELECT statement SELECT statement template SELECT FROM SELECT statement template SELECT FROM

11 SELECT statement SELECT statement template SELECT FROM WHERE SELECT statement template SELECT FROM WHERE

12 SELECT statement Basic query

13 SELECT statement Basic query “Find full information about all suppliers” Basic query “Find full information about all suppliers”

14 SELECT statement Basic query “Find full information about all suppliers” SELECTs#, sname, city, dob FROMSupplier or Basic query “Find full information about all suppliers” SELECTs#, sname, city, dob FROMSupplier or

15 SELECT statement Basic query “Find full information about all suppliers” SELECTs#, sname, city, dob FROMSupplier or SELECT* FROM Supplier; Basic query “Find full information about all suppliers” SELECTs#, sname, city, dob FROMSupplier or SELECT* FROM Supplier;

16 SELECT statement Projection query

17 SELECT statement Projection query “Find the numbers, names, and prices of all parts” Projection query “Find the numbers, names, and prices of all parts”

18 SELECT statement Projection query “Find the numbers, names, and prices of all parts” SELECTp#, pname, price FROM Part; Projection query “Find the numbers, names, and prices of all parts” SELECTp#, pname, price FROM Part;

19 SELECT statement Projection query with duplicates

20 SELECT statement Projection query with duplicates “Find the numbers of all suppliers that shipped at least one part” Projection query with duplicates “Find the numbers of all suppliers that shipped at least one part”

21 SELECT statement Projection query with duplicates “Find the numbers of all suppliers that shipped at least one part” SELECTs# FROM SP; Projection query with duplicates “Find the numbers of all suppliers that shipped at least one part” SELECTs# FROM SP;

22 SELECT statement Projection query with no duplicates

23 SELECT statement Projection query with no duplicates “Find the numbers of all suppliers that shipped at least one part” Projection query with no duplicates “Find the numbers of all suppliers that shipped at least one part”

24 SELECT statement Projection query with no duplicates “Find the numbers of all suppliers that shipped at least one part” SELECTDISTINCT s# FROM SP; Projection query with no duplicates “Find the numbers of all suppliers that shipped at least one part” SELECTDISTINCT s# FROM SP;

25 SELECT statement Queries with standard functions

26 SELECT statement Queries with standard functions “Find the total number of shipments” Queries with standard functions “Find the total number of shipments”

27 SELECT statement Queries with standard functions “Find the total number of shipments” SELECTcount(*) FROM SP; Queries with standard functions “Find the total number of shipments” SELECTcount(*) FROM SP;

28 SELECT statement Queries with standard functions “Find the total number of all parts shipped” Queries with standard functions “Find the total number of all parts shipped”

29 SELECT statement Queries with standard functions “Find the total number of all parts shipped” SELECTsum(quantity) FROM SP; Queries with standard functions “Find the total number of all parts shipped” SELECTsum(quantity) FROM SP;

30 SELECT statement Queries with standard functions “Find an average price of all parts” Queries with standard functions “Find an average price of all parts”

31 SELECT statement Queries with standard functions “Find an average price of all parts” SELECTavg(price) FROM Part; Queries with standard functions “Find an average price of all parts” SELECTavg(price) FROM Part;

32 SELECT statement Queries with standard functions “Find the highest and the lowest price” Queries with standard functions “Find the highest and the lowest price”

33 SELECT statement Queries with standard functions “Find the highest and the lowest price” SELECTmax(price), min(price) FROM Part; Queries with standard functions “Find the highest and the lowest price” SELECTmax(price), min(price) FROM Part;

34 SELECT statement Queries with elementary condition

35 SELECT statement Queries with elementary condition “Find the names and numbers of all gold parts” Queries with elementary condition “Find the names and numbers of all gold parts”

36 SELECT statement Queries with elementary condition “Find the names and numbers of all gold parts” SELECTp#, pname FROM Part WHERE colour = ‘gold’; Queries with elementary condition “Find the names and numbers of all gold parts” SELECTp#, pname FROM Part WHERE colour = ‘gold’;

37 SELECT statement Queries with elementary condition “Find the names and dates of birth of all suppliers born before 1960” Queries with elementary condition “Find the names and dates of birth of all suppliers born before 1960”

38 SELECT statement Queries with elementary condition “Find the names and dates of birth of all suppliers born before 1960” SELECTsname, dob FROM Supplier WHEREdob < ‘1-Jan-1960’; Queries with elementary condition “Find the names and dates of birth of all suppliers born before 1960” SELECTsname, dob FROM Supplier WHEREdob < ‘1-Jan-1960’;

39 SELECT statement Queries with elementary condition “Find full information about all suppliers living in Paris “ Queries with elementary condition “Find full information about all suppliers living in Paris “

40 SELECT statement Queries with elementary condition “Find full information about all suppliers living in Paris “ SELECT* FROM Supplier WHERE city = ‘Paris’; Queries with elementary condition “Find full information about all suppliers living in Paris “ SELECT* FROM Supplier WHERE city = ‘Paris’;

41 SELECT statement Queries with elementary condition “Find full information about all suppliers whose name starts from letter ‘J’ “ Queries with elementary condition “Find full information about all suppliers whose name starts from letter ‘J’ “

42 SELECT statement Queries with elementary condition “Find full information about all suppliers whose name starts from letter ‘J’ “ SELECT* FROM Supplier WHERE sname LIKE ‘J%’; Queries with elementary condition “Find full information about all suppliers whose name starts from letter ‘J’ “ SELECT* FROM Supplier WHERE sname LIKE ‘J%’;

43 SELECT statement Queries with elementary condition “Find full information about all shipments done between 1990 and 1996” Queries with elementary condition “Find full information about all shipments done between 1990 and 1996”

44 SELECT statement Queries with elementary condition “Find full information about all shipments done between 1990 and 1996” SELECT* FROM SP WHERE sdate BETWEEN ‘01-Jan-1990’ AND 31-Dec-1996’; Queries with elementary condition “Find full information about all shipments done between 1990 and 1996” SELECT* FROM SP WHERE sdate BETWEEN ‘01-Jan-1990’ AND 31-Dec-1996’;

45 SELECT statement Queries with Boolean expression

46 SELECT statement Queries with Boolean expression “Find the prices of all bolts that cost more than 50.0” Queries with Boolean expression “Find the prices of all bolts that cost more than 50.0”

47 SELECT statement Queries with Boolean expression “Find the prices of all bolts that cost more than 50.0” SELECTprice FROM Part WHERE(pname = ‘bolt’) AND (price > 50.0); Queries with Boolean expression “Find the prices of all bolts that cost more than 50.0” SELECTprice FROM Part WHERE(pname = ‘bolt’) AND (price > 50.0);

48 SELECT statement Queries with Boolean expression “Find the prices of all gold or silver bolts” Queries with Boolean expression “Find the prices of all gold or silver bolts”

49 SELECT statement Queries with Boolean expression “Find the prices of all gold or silver bolts” SELECTprice FROM Part WHERE(pname = ‘bolt’) AND ( (colour = ‘gold’) OR (colour = ‘silver’)); Queries with Boolean expression “Find the prices of all gold or silver bolts” SELECTprice FROM Part WHERE(pname = ‘bolt’) AND ( (colour = ‘gold’) OR (colour = ‘silver’));

50 SELECT statement Queries with Boolean expression “Find the prices of all gold or silver bolts” SELECTprice FROM Part WHERE(pname = ‘bolt’) AND (colour in (‘gold’, ‘silver’)); Queries with Boolean expression “Find the prices of all gold or silver bolts” SELECTprice FROM Part WHERE(pname = ‘bolt’) AND (colour in (‘gold’, ‘silver’));

51 SELECT statement Queries with Boolean expression “Find the prices of all bolts except gold and silver” Queries with Boolean expression “Find the prices of all bolts except gold and silver”

52 SELECT statement Queries with Boolean expression “Find the prices of all bolts except gold and silver” SELECTprice FROM Part WHERE(pname = ‘bolt’) AND (colour NOT IN (‘gold’, ‘silver’)); Queries with Boolean expression “Find the prices of all bolts except gold and silver” SELECTprice FROM Part WHERE(pname = ‘bolt’) AND (colour NOT IN (‘gold’, ‘silver’));

53 SELECT statement Queries with Boolean expression “Find the prices of all parts except silver bolts” Queries with Boolean expression “Find the prices of all parts except silver bolts”

54 SELECT statement Queries with Boolean expression “Find the prices of all parts except silver bolts” SELECTprice FROM Part WHERENOT( (pname = ‘bolt’) AND (colour = ‘silver’) ); Queries with Boolean expression “Find the prices of all parts except silver bolts” SELECTprice FROM Part WHERENOT( (pname = ‘bolt’) AND (colour = ‘silver’) );

55 SELECT statement Queries with Boolean expression “Find the prices of all parts except silver bolts” SELECTprice FROM Part WHERE(pname <> ‘bolt’) OR (colour <> ‘silver’); Queries with Boolean expression “Find the prices of all parts except silver bolts” SELECTprice FROM Part WHERE(pname <> ‘bolt’) OR (colour <> ‘silver’);

56 SELECT statement Sorting

57 SELECT statement Sorting “Find all suppliers sorted by names in ascending order Sorting “Find all suppliers sorted by names in ascending order

58 SELECT statement Sorting “Find all suppliers sorted by names in ascending order SELECT* FROM Supplier ORDER BY sname ASC; Sorting “Find all suppliers sorted by names in ascending order SELECT* FROM Supplier ORDER BY sname ASC;

59 SELECT statement Sorting “Find all parts sorted by prices in descending order” Sorting “Find all parts sorted by prices in descending order”

60 SELECT statement Sorting “Find all parts sorted by prices in descending order” SELECT * FROM Part ORDER BY price DESC; Sorting “Find all parts sorted by prices in descending order” SELECT * FROM Part ORDER BY price DESC;

61 SELECT statement Queries with NULL values

62 SELECT statement Queries with NULL values “Find full information about all suppliers with unknown date of birth” Queries with NULL values “Find full information about all suppliers with unknown date of birth”

63 SELECT statement Queries with NULL values “Find full information about all suppliers with unknown date of birth” SELECT* FROM Supplier WHERE dob IS NULL; Queries with NULL values “Find full information about all suppliers with unknown date of birth” SELECT* FROM Supplier WHERE dob IS NULL;

64 SELECT statement Queries with NULL values “Find full information about all suppliers with known date of birth” Queries with NULL values “Find full information about all suppliers with known date of birth”

65 SELECT statement Queries with NULL values “Find full information about all suppliers with known date of birth” SELECT* FROM Supplier WHERE dob IS NOT NULL; Queries with NULL values “Find full information about all suppliers with known date of birth” SELECT* FROM Supplier WHERE dob IS NOT NULL;

66 SELECT statement Bibliography  R. K. Stephens, et al. Teach Yourself SQL in 21 Days, day 9, week 2  P. O’Neil, Database - Principles, Programming, Performance, chapter 3.3  R. Elmasri, S.B. Navathe, Fundamentals of Database Systems, chapter.2 Bibliography  R. K. Stephens, et al. Teach Yourself SQL in 21 Days, day 9, week 2  P. O’Neil, Database - Principles, Programming, Performance, chapter 3.3  R. Elmasri, S.B. Navathe, Fundamentals of Database Systems, chapter.2