Chapter 3 Part3
Chapter 3 Part3
Content
3.1. How to design a database
3.2. How to using SQL to create a MySQL database
3.3. How to using SQL to work with a MySQL database
3.4. Professional PHP for working with MySQL
3.5. A database-driven website
C1, Slide 2
3.3. How to using SQL to work with a MySQL
database
Objectives
Applied
1. Code and run SELECT statements that use any of the language
elements presented in this chapter.
2. Code and run INSERT, UPDATE, or DELETE statements that
add, update, or delete rows.
C18, Slide 3
Objectives (continued)
Knowledge
1. Describe the use of the SELECT and FROM clauses in a SELECT
statement, including the use of column aliases.
2. Describe the use of the LIMIT clause in a SELECT statement.
3. Describe the use of the WHERE clause in a SELECT statement,
including the use of comparison operators, logical operators, the
IS NULL operator, and the LIKE operator.
4. Describe the use of the ORDER BY clause in a SELECT
statement.
5. Describe the use of an inner join, including one that uses table
aliases.
6. Describe the use of aggregate functions in SELECT statements.
C18, Slide 4
Objectives (continued)
Knowledge
7. Describe the use of the GROUP BY and HAVING clauses in a
SELECT statement, and distinguish between HAVING clauses
and WHERE clauses.
8. Describe the way subqueries can be used in the WHERE,
HAVING, FROM and SELECT clauses of a SELECT statement.
9. Describe the use of INSERT, UPDATE, and DELETE
statements, including the handling of null values and default
values when coding INSERT and UPDATE statements.
C18, Slide 5
The simplified syntax of the SELECT
statement
SELECT select_list
FROM table_source
[WHERE search_condition]
[ORDER BY order_by_list]
C18, Slide 6
Retrieve all rows and columns from a table
SELECT * FROM products
(3 rows of 10)
C18, Slide 7
Retrieve three columns and sort them by price
SELECT productID, productName, listPrice
FROM products
ORDER BY listPrice
(6 rows of 10)
C18, Slide 8
Retrieve rows in the specified price range
SELECT productID, productName, listPrice
FROM products
WHERE listPrice < 450
ORDER BY listPrice
C18, Slide 9
Retrieve an empty result set
SELECT productID, productName, listPrice
FROM products
WHERE listPrice < 10
C18, Slide 10
Use the AS keyword to specify an alias
SELECT productID,
productName AS name,
listPrice AS price
FROM products
WHERE listPrice < 450
ORDER BY listPrice
C18, Slide 11
Omitting the AS keyword works too
SELECT productID, productName name, listPrice price
FROM products
WHERE listPrice < 450
ORDER BY listPrice
C18, Slide 12
Use quotes to include spaces
SELECT productID AS "ID",
productName AS "Product Name",
listPrice AS "Price"
FROM products
WHERE listPrice < 450
ORDER BY listPrice
C18, Slide 13
The syntax of the LIMIT clause
LIMIT [rowOffset, ] maxRows
C18, Slide 14
Another way to retrieve the first three rows
SELECT productID, productName
FROM products
LIMIT 0, 3
C18, Slide 15
Retrieve three rows starting at the second row
SELECT productID, productName
FROM products
LIMIT 1, 3
C18, Slide 16
The syntax of the WHERE clause
with comparison operators
WHERE expression_1 operator expression_2
C18, Slide 17
A WHERE clause that selects products
where the product…
Is in the specified category
WHERE categoryID = 2
C18, Slide 18
A WHERE clause that selects products
where the product…
Has a name that starts with the letters A to F
WHERE productName < 'G'
C18, Slide 19
The syntax of the WHERE clause
with logical operators
WHERE [NOT] search_condition_1
{AND|OR} [NOT] search_condition_2 ...
C18, Slide 20
A compound condition without parentheses
SELECT productName, listPrice, discountPercent, dateAdded
FROM products
WHERE dateAdded > '2017-07-01' OR listPrice < 500
AND discountPercent > 25
C18, Slide 21
The same compound condition with
parentheses
SELECT productName, listPrice, discountPercent, dateAdded
FROM products
WHERE (dateAdded > '2017-07-01' OR listPrice < 500)
AND discountPercent > 25
C18, Slide 22
The syntax of the WHERE clause
with the IS NULL operator
WHERE expression IS [NOT] NULL
C18, Slide 23
Retrieve rows for orders
that haven’t been shipped
SELECT orderID, orderDate, shipDate
FROM orders
WHERE shipDate IS NULL
C18, Slide 24
Retrieve rows for orders that have been
shipped
SELECT orderID, orderDate, shipDate
FROM orders
WHERE shipDate IS NOT NULL
C18, Slide 25
The syntax of the WHERE clause
with the LIKE operator
WHERE match_expression [NOT] LIKE pattern
Wildcard symbols
%
_
C18, Slide 26
WHERE clauses that use the LIKE operator
WHERE productName LIKE 'Fender%'
WHERE productName LIKE '%cast%'
WHERE zipCode LIKE '076__'
WHERE orderDate LIKE '2017-06-__%'
C18, Slide 27
The syntax of the ORDER BY clause
ORDER BY expression [ASC|DESC]
[, expression [ASC|DESC]] ...
C18, Slide 28
Sort by one column in descending sequence
SELECT productName, listPrice, discountPercent
FROM products
WHERE listPrice < 500
ORDER BY listPrice DESC
C18, Slide 29
Sort by two columns
SELECT productName, listPrice, discountPercent
FROM products
WHERE categoryID = 1
ORDER BY discountPercent, listPrice DESC
C18, Slide 30
The explicit syntax for an inner join
SELECT select_list
FROM table_1
[INNER] JOIN table_2
ON join_condition_1
[[INNER] JOIN table_3
ON join_condition_2] ...
C18, Slide 31
Joining the customers and orders tables
SELECT firstName, lastName, orderDate
FROM customers
INNER JOIN orders
ON customers.customerID = orders.customerID
ORDER BY orderDate
C18, Slide 32
The syntax for an inner join that uses table
aliases
SELECT select_list
FROM table_1 AS n1
[INNER] JOIN table_2 [AS] n2
ON n1.column_name operator n2.column_name
[[INNER] JOIN table_3 [AS] n3
ON n2.column_name operator n3.column_name]...
C18, Slide 33
An inner join with aliases for four tables
SELECT firstName, lastName, o.orderID,
productName, itemPrice, quantity
FROM customers c
INNER JOIN orders o
ON c.customerID = o.customerID
INNER JOIN orderItems oi
ON o.orderID = oi.orderID
INNER JOIN products p
ON oi.productID = p.productID
ORDER BY o.orderID
C18, Slide 34
The syntax of the aggregate functions
AVG(expression)
SUM(expression)
MIN(expression)
MAX(expression)
COUNT(expression)
COUNT(*)
C18, Slide 35
Count all orders and shipped orders
SELECT COUNT(*) AS totalCount,
COUNT(shipDate) AS shippedCount
FROM orders
C18, Slide 36
Find lowest, highest, and average prices
SELECT MIN(listPrice) AS lowestPrice,
MAX(listPrice) AS highestPrice,
AVG(listPrice) AS averagePrice
FROM products
C18, Slide 37
Get the total of the calculated values for all
orders
SELECT SUM(itemPrice * quantity – discountAmount)
AS ordersTotal
FROM orderItems
C18, Slide 38
The syntax of the GROUP BY and HAVING
clauses
SELECT select_list
FROM table_source
[WHERE search_condition]
[GROUP BY group_by_list]
[HAVING search_condition]
[ORDER BY order_by_list]
C18, Slide 39
Use columns from multiple tables
SELECT categoryName, COUNT(*) AS productCount,
AVG(listPrice) AS averageListPrice
FROM products p JOIN categories c
ON p.categoryID = c.categoryID
GROUP BY categoryName
HAVING averageListPrice > 400
C18, Slide 40
Use a WHERE clause to filter rows
before grouping them
SELECT categoryName, COUNT(*) AS productCount,
AVG(listPrice) AS averageListPrice
FROM products p JOIN categories c
ON p.categoryID = c.categoryID
WHERE listPrice > 400
GROUP BY categoryName
C18, Slide 41
Four ways to introduce a subquery
in a SELECT statement
In a WHERE clause as a search condition
In a HAVING clause as a search condition
In the FROM clause as a table specification
In the SELECT clause as a column specification
C18, Slide 42
Use a subquery in the WHERE clause
SELECT productName, listPrice
FROM products
WHERE listPrice > (SELECT AVG(listPrice) FROM products)
ORDER BY listPrice DESC
C18, Slide 43
Use another subquery in the WHERE clause
SELECT productName, listPrice
FROM products
WHERE categoryID = (SELECT categoryID FROM categories
WHERE categoryName = 'Basses')
C18, Slide 44
A correlated subquery in the SELECT clause
SELECT categoryID, categoryName,
(SELECT COUNT(*) FROM products
WHERE products.categoryID = categories.categoryID)
AS productCount
FROM categories
C18, Slide 45
The syntax of a subquery
that uses the EXISTS clause
WHERE [NOT] EXISTS (subquery)
C18, Slide 46
The syntax of the INSERT statement
INSERT INTO table_name [(column_list)]
VALUES (expression_1 [, expression_2]...)[,
(expression_1 [, expression_2]...)]...
C18, Slide 47
Add a single row without using a column list
INSERT INTO products
VALUES (DEFAULT, 1, 'tele', 'Fender Telecaster', 'NA',
'949.99', DEFAULT, NOW())
C18, Slide 48
The syntax of the UPDATE statement
UPDATE table_name
SET column_name_1 = expression_1
[, column_name_2 = expression_2]...
[WHERE search_condition]
C18, Slide 49
Update one column of multiple rows
UPDATE products
SET discountPercent = '15.00'
WHERE categoryID = 2
Warning
If you omit the WHERE clause, all rows in the table are updated.
C18, Slide 50
The syntax of the DELETE statement
DELETE [FROM] table_name
[WHERE search_condition]
C18, Slide 51
Another way to delete multiple rows
DELETE FROM categories
WHERE categoryID > 3
Warning
If you omit the WHERE clause, all rows in the table are deleted.
C18, Slide 52