![Page 1: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/1.jpg)
A Guide to MySQL
4
![Page 2: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/2.jpg)
A Guide to MySQL 2
Objectives
• Retrieve data from a database using SQL commands
• Use compound conditions in queries
• Use computed columns in queries
• Use the SQL LIKE operator
• Use the SQL IN operator
• Sort data using the ORDER BY clause
![Page 3: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/3.jpg)
A Guide to MySQL 3
Objectives (continued)
• Sort data using multiple keys and in ascending and descending order
• Use SQL aggregate functions
• Use subqueries
• Group data using the GROUP BY clause
• Select individual groups of data using the HAVING clause
• Retrieve columns with null values
![Page 4: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/4.jpg)
A Guide to MySQL 4
Constructing Simple Queries
• Query: question represented in a way that the DBMS can understand
• To implement in MySQL, use SELECT command
• No special formatting rules
![Page 5: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/5.jpg)
A Guide to MySQL 5
Constructing Simple Queries (continued)
• SELECT-FROM-WHERE statement:
– SELECT columns to include in result
– FROM table containing columns
– WHERE any conditions to apply to the data
![Page 6: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/6.jpg)
A Guide to MySQL 6
Retrieving Certain Columns and All Rows
• Use SELECT command to retrieve specified columns and all rows; e.g., list the number, name and balance of all customers
• No WHERE clause needed, because all customers are requested
![Page 7: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/7.jpg)
A Guide to MySQL 7
Retrieving Certain Columns and All Rows (continued)
![Page 8: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/8.jpg)
A Guide to MySQL 8
Retrieving All Columns and All Rows
• Use an asterisk (*) to indicate all columns in the SELECT clause
• Will list all columns in the order used when table was created
• List specific columns in SELECT clause to present columns in a different order
![Page 9: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/9.jpg)
A Guide to MySQL 9
Retrieving All Columns and All Rows (continued)
![Page 10: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/10.jpg)
A Guide to MySQL 10
Using a WHERE Clause
• WHERE clause:
– Used to retrieve rows that satisfy some condition
– What is the name of customer number 148?
• Simple condition: column name, comparison operator followed by either a column name or a value
![Page 11: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/11.jpg)
A Guide to MySQL 11
Using a WHERE Clause (continued)
![Page 12: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/12.jpg)
A Guide to MySQL 12
Using a WHERE Clause (continued)
![Page 13: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/13.jpg)
A Guide to MySQL 13
Using a WHERE Clause (continued)
![Page 14: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/14.jpg)
A Guide to MySQL 14
Using Compound Conditions
• Compound condition: connects two or more simple conditions with AND, OR, and NOT operators
• AND operator: all simple conditions are true
• OR operator: any simple condition is true
• NOT operator: reverses the truth of the original condition
![Page 15: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/15.jpg)
A Guide to MySQL 15
Using Compound Conditions (continued)
![Page 16: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/16.jpg)
A Guide to MySQL 16
Using Compound Conditions (continued)
![Page 17: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/17.jpg)
A Guide to MySQL 17
Using Compound Conditions (continued)
![Page 18: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/18.jpg)
A Guide to MySQL 18
Using Compound Conditions (continued)
![Page 19: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/19.jpg)
A Guide to MySQL 19
Using the BETWEEN Operator
• Use instead of AND operator
• Use when searching a range of values
• Makes SELECT commands simpler to construct
• Inclusive: when using BETWEEN 2000 and 5000, values of 2000 or 5000 would be true
![Page 20: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/20.jpg)
A Guide to MySQL 20
Using the BETWEEN Operator (continued)
![Page 21: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/21.jpg)
A Guide to MySQL 21
Using the BETWEEN Operator (continued)
![Page 22: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/22.jpg)
A Guide to MySQL 22
Using Computed Columns• Computed column: does not exist in the database
but is computed using data in existing columns
• Arithmetic operators:
– + for addition
– - for subtraction
– * for multiplication
– / for division
![Page 23: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/23.jpg)
A Guide to MySQL 23
Using Computed Columns (continued)
![Page 24: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/24.jpg)
A Guide to MySQL 24
Using Computed Columns (continued)
![Page 25: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/25.jpg)
A Guide to MySQL 25
Using the LIKE Operator
• Used for pattern matching
• LIKE %Central% will retrieve data with those characters; e.g., “3829 Central” or “Centralia”
• Underscore (_) represents any single character; e.g., “T_M” for TIM or TOM or T3M
![Page 26: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/26.jpg)
A Guide to MySQL 26
Using the IN Operator
![Page 27: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/27.jpg)
A Guide to MySQL 27
Sorting
• By default, no defined order in which results are displayed
• Use ORDER BY clause to list data in a specific order
![Page 28: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/28.jpg)
A Guide to MySQL 28
Using the ORDER BY Clause
• Sort key or key: column on which data is to be sorted
• Ascending is default sort order
![Page 29: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/29.jpg)
A Guide to MySQL 29
Additional Sorting Options
• Possible to sort data by more than one key
• Major sort key and minor sort key
• List sort keys in order of importance in the ORDER BY clause
• For descending order sort, use DESC
![Page 30: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/30.jpg)
A Guide to MySQL 30
Additional Sorting Options (continued)
![Page 31: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/31.jpg)
A Guide to MySQL 31
Using Functions
![Page 32: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/32.jpg)
A Guide to MySQL 32
Using the COUNT Function
![Page 33: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/33.jpg)
A Guide to MySQL 33
Using the SUM Function
• Used to calculate totals of columns
• Column must be specified and must be numeric
• Null values are ignored
![Page 34: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/34.jpg)
A Guide to MySQL 34
Using the AVG, MAX, and MIN Functions
![Page 35: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/35.jpg)
A Guide to MySQL 35
Using the DISTINCT Operator
• Eliminates duplicate values• Used with COUNT function
![Page 36: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/36.jpg)
A Guide to MySQL 36
Using the DISTINCT Operator (continued)
![Page 37: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/37.jpg)
A Guide to MySQL 37
Using the DISTINCT Operator (continued)
![Page 38: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/38.jpg)
A Guide to MySQL 38
Nesting Queries
• Query results require two or more steps
• Subquery: an inner query placed inside another query
• Outer query uses subquery results
![Page 39: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/39.jpg)
A Guide to MySQL 39
Nesting Queries (continued)
![Page 40: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/40.jpg)
A Guide to MySQL 40
Nesting Queries (continued)
![Page 41: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/41.jpg)
A Guide to MySQL 41
Grouping
• Grouping: creates groups of rows that share common characteristics
• Calculations in the SELECT command are performed for the entire group
![Page 42: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/42.jpg)
A Guide to MySQL 42
Using the GROUP BY Clause
![Page 43: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/43.jpg)
A Guide to MySQL 43
Using a HAVING Clause
![Page 44: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/44.jpg)
A Guide to MySQL 44
HAVING vs. WHERE
• WHERE: limit rows• HAVING: limit groups• Can use together if condition involves both rows and
groups
![Page 45: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/45.jpg)
A Guide to MySQL 45
HAVING vs. WHERE (continued)
![Page 46: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/46.jpg)
A Guide to MySQL 46
Nulls
![Page 47: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/47.jpg)
A Guide to MySQL 47
Summary• Create queries that retrieve data from a single table
using SELECT commands
• Comparison operators: =, >,=>,<,=<, or <>, or !=
• Compound conditions: AND,OR, and NOT
• Use the BETWEEN operator
• Use the LIKE operator
![Page 48: A Guide to MySQL 4. 2 Objectives Retrieve data from a database using SQL commands Use compound conditions in queries Use computed columns in queries Use](https://reader036.vdocuments.mx/reader036/viewer/2022062620/551b10a05503465e7d8b6025/html5/thumbnails/48.jpg)
A Guide to MySQL 48
Summary• IN operator
• ORDER BY clause
• Aggregate functions:
– COUNT, SUM, AVG, MAX, and MIN
– DISTINCT operator
• Subqueries
• GROUP BY
• NULL