SQL Commands Flashcards
What does the “AS” command do?
allows you to rename a column or table
SELECT column_name AS ‘alias’ from table_name;
What does AVG do?
Returns the average of the numeric value SELECT AVG(column_name_ from table_name
What does the BETWEEN command do?
Filters the result between certain range
SELECT column_name from table_name WHERE column_name BETWEEN value_1 and value_2
What does the CASE command do?
If-then logic to create new variables
SELECT column_name, CASE WHEN condition THEN 'Result_1' WHEN condition THEN 'Result_2' ELSE 'Result_3' END FROM table_name
COUNT
Count non-null rows in a column
SELECT COUNT(column_name_ from table_name;
GROUP BY
Aggregate function
Organizes the output into different groups
SELECT column_name
FROM table_name
GROUP BY column_name
HAVING
Is “WHERE” with aggregate functions
SELECT column_name, COUNT()
FROM table_name
GROUP BY column_name
HAVING COUNT() > value;
Website with these commands
https://www.codecademy.com/articles/sql-commands
Inner Join
Combine different rows from different tables if the JOIN condition is true
SELECT column_name
FROM table_name1
JOIN table_2
ON table_1.column_name = table_2.column_name;
IS NULL / IS NOT NULL
Used with WHERE clause to test for empty values
SELECT column_name
FROM table_name
WHERE column_name IS NOT NULL
LIKE
Special operator used with the WHERE caluse to search for a specific pattern in a column
SELECT column_name
FROM table_name
WHERE column_name LIKE %pulse%
LIMIT
Clause that lets you specific the maximum number of rows the result set will have
SELECT column_name
FROM table_name
LIMIT number;
MAX
function that returns the largest value in a column
SELECT MAX(column_name) FROM table_name;
MIN
Function that returns the smallest value in a column
SELECT MIN(column_name) FROM table_name
OR
Operator that filters the results set to only include where where either condition is true
SELECT column_name
FROM table_name
WHERE column_name = value_1
OR column_name2=value_2
ORDER BY
Clause that allows you to order the results by a particular column in ascending or descending order
SELECT column_name
FROM table_name
ORDER BY column_name ASC | DESC
LEFT JOIN
Combine rows from different tables even if the join condition is not met.
With left join, all of the rows on the left table is returned.
SELECT column_name
FROM table_name
LEFT JOIN table_2
ON table_1.column_name = table_2.column_name
RIGHT JOIN
Combines rows from different tables even if the join condition is not met.
With right join, all of the rows on the right table are returned. NULL for left table
SELECT column_name
FROM table_1
RIGHT JOIN table_2
ON table_1.column_name = table_2.column_name
FULL JOIN
Returns all records when there is a match in the left or right table records.
SELECT column_name
FROM table1
FULL JOIN table2
ON table1.column_name = table2.column_name;
ROUND
Function that rounds the values in a column to the integer specified in the function argument.
SELECT ROUND(column_name, integer) FROM table_name
SELECT DISTINCT
specifies that the statement is going to be a query that returns unique values in the specified columns
SELECT DISTINCT column_name
FROM table_name;
SUM
Function that adds a column
SELECT SUM(column_name) FROM table_name
Comments in SQL
/*
*/
How to create subqueries (nested queries, inner queries)
SELECT column_name,
(Select AVG(salary) from table_name as NAME from table_name)
FROM table_name
Where can you use subqueries?
SELECT, FROM, WHERE
Basically anywhere