Test 3
Test 3
Query a database
Apply filters to SQL queries
Join tables
Perform calculations
Query a database
The SELECT, FROM, and ORDER BY keywords are used when retrieving information from a
database.
FROM
Indicates which table to query; required to perform a query
FROM employees
Indicates to query the employees table
ORDER BY
Sequences the records returned by a query based on a specified column or columns
ORDER BY department
Sorts the records in ascending order by the department column; ORDER BY department
ASC also sorts the records in ascending order by the department column
SELECT employee_id
Returns the employee_id column
SELECT *
Returns all columns in a table
Apply filters to SQL queries
WHERE and the other SQL keywords and characters that follow are used when applying
filters to SQL queries.
AND
Specifies that both conditions must be met simultaneously in a filter that contains
two conditions
'a%'
Represents a pattern consisting of the letter 'a' followed by zero or more
characters
'%a'
Represents a pattern consisting of zero or more characters followed by the letter
'a'
'%a%'
Represents a pattern consisting of the letter 'a' surrounded by zero or more
characters on each side
_ (underscore)
Substitutes for one other character; used as a wildcard in a pattern that follows
LIKE
'a_'
Represents a pattern consisting of the letter 'a' followed by one character
'a__'
Represents a pattern consisting of the letter 'a' followed by two characters
'_a'
Represents a pattern consisting of one character followed by the letter 'a'
'_a_'
Represents a pattern consisting of the letter 'a' surrounded by one character on
each side
WHERE
Indicates the condition for a filter; must be used to begin a filter
SELECT *
FROM employees
FULL OUTER JOIN machines ON employees.device_id = machines.device_id;
Returns all records from the employees table and machines table; uses the device_id
column to join the two tables
INNER JOIN
Returns records matching on a specified column that exists in more than one table;
the column used to join the tables is specified following INNER JOIN with syntax
that includes ON and equal to (=)
SELECT *
FROM employees
INNER JOIN machines ON employees.device_id = machines.device_id;
Returns all records that have a value in the device_id column in the employees
table that matches a value in the device_id column in the machines table
LEFT JOIN
Returns all the records of the first table, but only returns records of the second
table that match on a specified column; the first (or left) table appears directly
after the keyword FROM; the column used to join the tables is specified following
LEFT JOIN with syntax that includes ON and equal to (=)
SELECT *
FROM employees
LEFT JOIN machines ON employees.device_id = machines.device_id;
Returns all records from the employees table but only the records from the machines
table that have a value in the device_id column that matches a value in the
device_id column in the employees table
RIGHT JOIN
Returns all of the records of the second table, but only returns records from the
first table that match on a specified column; the second (or right) table appears
directly after the RIGHT JOIN keyword; the column used to join the tables is
specified following RIGHT JOIN with syntax that includes ON and equal to (=)
SELECT *
FROM employees
RIGHT JOIN machines ON employees.device_id = machines.device_id;
Returns all records from the machines table but only the records from the employees
table that have a value in the device_id column that matches a value in the
device_id column in the machines table
Perform calculations
The following SQL keywords are aggregate functions and are helpful when performing
calculations.
AVG
Returns a single number that represents the average of the numerical data in a
column; placed after SELECT
SELECT AVG(height)
Returns the average height from all records that have a value in the height column
COUNT
Returns a single number that represents the number of records returned from a
query; placed after SELECT
SELECT COUNT(firstname)
Returns the number of records that have a value in the firstname column
SUM
Returns a single number that represents the sum of the numerical data in a column;
placed after SELECT
SELECT SUM(cost)
Returns the sum of costs from all records that have a value in the cost column