Database Queries and Structured Query Language (SQL) J.G. Zheng May 16 th 2008.
-
Upload
gabriella-dougherty -
Category
Documents
-
view
216 -
download
1
Transcript of Database Queries and Structured Query Language (SQL) J.G. Zheng May 16 th 2008.
![Page 1: Database Queries and Structured Query Language (SQL) J.G. Zheng May 16 th 2008.](https://reader036.fdocuments.net/reader036/viewer/2022082604/5514909d550346ea6e8b51b4/html5/thumbnails/1.jpg)
Database Queries and Structured Query Language(SQL)
J.G. ZhengMay 16th 2008
![Page 2: Database Queries and Structured Query Language (SQL) J.G. Zheng May 16 th 2008.](https://reader036.fdocuments.net/reader036/viewer/2022082604/5514909d550346ea6e8b51b4/html5/thumbnails/2.jpg)
2
Overview
SQL Language Query with SQL
All examples can be used with the “AmazonBook” database Download it from the course website
![Page 3: Database Queries and Structured Query Language (SQL) J.G. Zheng May 16 th 2008.](https://reader036.fdocuments.net/reader036/viewer/2022082604/5514909d550346ea6e8b51b4/html5/thumbnails/3.jpg)
3
Introduction/Review
What is SQL Review: Database and DBMS (chapter 1) SQL is a standard language accepted by
relational DBMS to perform database operations
Some facts about SQL SQL 92 is the most commonly supported version English-like (not programming) Case insensitive Venders have different implementations
![Page 4: Database Queries and Structured Query Language (SQL) J.G. Zheng May 16 th 2008.](https://reader036.fdocuments.net/reader036/viewer/2022082604/5514909d550346ea6e8b51b4/html5/thumbnails/4.jpg)
4
Concepts Review
Relational model concepts reviewRelation : TableRow : RecordColumn : Field (Attribute)
![Page 5: Database Queries and Structured Query Language (SQL) J.G. Zheng May 16 th 2008.](https://reader036.fdocuments.net/reader036/viewer/2022082604/5514909d550346ea6e8b51b4/html5/thumbnails/5.jpg)
5
Common Data Types
Numeric INTEGER, SMALLINT DECIMAL(i,j), NUMBER(i,j)
Character/String CHAR(n), VARCHAR(n)
Date DATE, DATETIME
See textbook page 42-43 for data types implemented in Access
![Page 6: Database Queries and Structured Query Language (SQL) J.G. Zheng May 16 th 2008.](https://reader036.fdocuments.net/reader036/viewer/2022082604/5514909d550346ea6e8b51b4/html5/thumbnails/6.jpg)
6
SELECT Statement
SELECT statement retrieves data from database (query) The result is usually another table
We will learn Defining selection criteria Sorting Calculation Grouping
![Page 7: Database Queries and Structured Query Language (SQL) J.G. Zheng May 16 th 2008.](https://reader036.fdocuments.net/reader036/viewer/2022082604/5514909d550346ea6e8b51b4/html5/thumbnails/7.jpg)
7
SELECT Statement Syntax
SELECT Column(s) or other expressionsFROM Table(s)[WHERE …][ORDER BY ColumnName]
![Page 8: Database Queries and Structured Query Language (SQL) J.G. Zheng May 16 th 2008.](https://reader036.fdocuments.net/reader036/viewer/2022082604/5514909d550346ea6e8b51b4/html5/thumbnails/8.jpg)
8
Simple SELECT Statement
Syntax SELECT * (or a list of columns)
FROM TableName Wild card: *
Example SELECT * FROM Books SELECT BookTitle, ListPrice FROM
Books
![Page 9: Database Queries and Structured Query Language (SQL) J.G. Zheng May 16 th 2008.](https://reader036.fdocuments.net/reader036/viewer/2022082604/5514909d550346ea6e8b51b4/html5/thumbnails/9.jpg)
9
Use WHERE Clause
Use WHERE clause to specify selection criteria
Example SELECT * FROM Books
WHERE ListPrice = 29.99 SELECT BookTitle, ListPrice FROM Books
WHERE ListPrice < 20
Comparison Operators “=“, “>”, “<“, “>=“, “<=“, “<>”
![Page 10: Database Queries and Structured Query Language (SQL) J.G. Zheng May 16 th 2008.](https://reader036.fdocuments.net/reader036/viewer/2022082604/5514909d550346ea6e8b51b4/html5/thumbnails/10.jpg)
10
More Comparison Operators
IN (value list) SELECT * FROM Books
WHERE ListPrice IN (19.99, 29.99, 39.99)
BETWEEN min AND max SELECT * FROM Books
WHERE ListPrice BETWEEN 9.99 AND 19.99
![Page 11: Database Queries and Structured Query Language (SQL) J.G. Zheng May 16 th 2008.](https://reader036.fdocuments.net/reader036/viewer/2022082604/5514909d550346ea6e8b51b4/html5/thumbnails/11.jpg)
11
String Pattern Match
Fuzzy query using LIKE _ (underscore): single character wildcard
? in Access % (percentage): multiple character wildcard
* in Access
Example SELECT * FROM Books
WHERE BookTitle LIKE '*information systems*'
![Page 12: Database Queries and Structured Query Language (SQL) J.G. Zheng May 16 th 2008.](https://reader036.fdocuments.net/reader036/viewer/2022082604/5514909d550346ea6e8b51b4/html5/thumbnails/12.jpg)
12
Compound Conditions
Use logical operators to connect multiple conditions
AND: an intersection of the data sets OR: a union of the data sets
Examples SELECT * FROM Books
WHERE ListPrice <= 19.99 AND ListPrice >= 9.99 SELECT * FROM Books
WHERE PubDate=#10/1/2003# OR PubDate=#10/1/2004#
SELECT * from Books WHERE Publisher = 'Que' AND Binding = 'Paperback'
![Page 13: Database Queries and Structured Query Language (SQL) J.G. Zheng May 16 th 2008.](https://reader036.fdocuments.net/reader036/viewer/2022082604/5514909d550346ea6e8b51b4/html5/thumbnails/13.jpg)
13
Sorting
Syntax ORDER BY Column(s) [ASC/DESC]
Examples SELECT * FROM Books
ORDER BY PubDate SELECT * FROM Books
ORDER BY Publisher DESC, PubDate
![Page 14: Database Queries and Structured Query Language (SQL) J.G. Zheng May 16 th 2008.](https://reader036.fdocuments.net/reader036/viewer/2022082604/5514909d550346ea6e8b51b4/html5/thumbnails/14.jpg)
14
Calculation
Calculating columns Calculated columns are not designed directly
into the table Using +, -, *, / with columns and numbers
Example SELECT BookTitle, ListPrice, ListPrice * 0.1 AS
DiscountFROM BooksWHERE ListPrice * 0.1 >= 15
![Page 15: Database Queries and Structured Query Language (SQL) J.G. Zheng May 16 th 2008.](https://reader036.fdocuments.net/reader036/viewer/2022082604/5514909d550346ea6e8b51b4/html5/thumbnails/15.jpg)
15
Built-in Functions
Using these functions to do statistics MIN MAX COUNT AVG SUM
Example SELECT COUNT(*) FROM Books SELECT AVG(ListPrice) FROM Books
WHERE Publisher = 'Prentice Hall'
![Page 16: Database Queries and Structured Query Language (SQL) J.G. Zheng May 16 th 2008.](https://reader036.fdocuments.net/reader036/viewer/2022082604/5514909d550346ea6e8b51b4/html5/thumbnails/16.jpg)
16
Grouping
GROUP BY: doing math with groups SELECT COUNT(*) FROM Books
WHERE Publisher = 'Prentice Hall';SELECT COUNT(*) FROM Books WHERE Publisher = 'The MIT Press';…
Or:SELECT Publisher, COUNT(*) FROM Books GROUP BY Publisher
![Page 17: Database Queries and Structured Query Language (SQL) J.G. Zheng May 16 th 2008.](https://reader036.fdocuments.net/reader036/viewer/2022082604/5514909d550346ea6e8b51b4/html5/thumbnails/17.jpg)
17
A Complete Query
SELECT ISBN, BookTitle, ListPrice, Publisher
FROM BooksWHERE
BookTitle like '*Information Systems*'AND PubDate > #1/1/2002#AND ListPrice < 100
ORDER BY ListPrice
![Page 18: Database Queries and Structured Query Language (SQL) J.G. Zheng May 16 th 2008.](https://reader036.fdocuments.net/reader036/viewer/2022082604/5514909d550346ea6e8b51b4/html5/thumbnails/18.jpg)
18
ExerciseUsing the “AmazonBook” database, use SQL or QBE to answer the following questions.
1. Which book is the most expensive? 2. How many books are under $100 of list price?3. I am looking for a “database” book, no more
than 50 dollars, and published after 1/1/2003; do you have any recommendations? I need the book title and its price.
4. What is the price per page for the books published by Que? List the book title, price, number of pages and price for page, and sort by the price per page, descending
![Page 19: Database Queries and Structured Query Language (SQL) J.G. Zheng May 16 th 2008.](https://reader036.fdocuments.net/reader036/viewer/2022082604/5514909d550346ea6e8b51b4/html5/thumbnails/19.jpg)
19
Good Resources
SQL Online Tutorial http://sqlcourse.com/ http://sqlcourse2.com/