mySQL Flashcards
Introduction
Get all columns from a table
SELECT *
FROM table
Get a column from a table
SELECT column
FROM table
Get multiple columns from table
SELECT col1, col2
FROM table
Use alias names
AS
SELECT col1, col2 AS col2_new
FROM table
Arrange the rows in asscending order of values in a column
ORDER BY
SELECT col1, col2
FROM table
ORDER BY col2
Arrange the rows in descending order of values in column
ORDER BY col DESC
SELECT col1, col2
FROM table
ORDER BY col2 DESC
Limit the number of rows returned
LIMIT
SELECT *
FROM table
LIMIT 2
Get unique values, filtering out duplicate rows, returning only unique rows.
DISTINCT
SELECT DISTINCT column
FROM table
Get rows where a number is greater than a value
WHERE col1 > n
SELECT col1
FROM table
WHERE col1 > value
Get rows where a number is greater than or equal to a value
WHERE col >=n
SELECT col1
FROM table
WHERE col1 >= value
Visualize Concatenating Columns with a New Name
SELECT CONCAT(first_name, ‘ ‘, last_name) AS full_name
FROM employees;
Visualize Using in Aggregations
SELECT department, SUM(salary) AS total_salary
FROM employees
GROUP BY department;
Get rows where a number is equal to a value
SELECT col1
FROM table
WHERE col1 = value
Get rows where a number is not equal ( WHERE col != n)
SELECT col1
FROM table
WHERE col1 <> value
Get rows where a number is between two values (inclusive)
SELECT col1
FROM table
WHERE col1 BETWEEN value1 AND value2
Get rows where text is equal to a value
SELECT col1, col2
FROM table
WHERE x = ‘string’
Get rows where text is one of several values
SELECT col1, col2
FROM table
WHERE x IN (‘string’, ‘string2’)
Get rows where text contains specific letters with WHERE col LIKE ‘%abc%’ (% represents any characters)
SELECT col1
FROM table
WHERE col1 LIKE ‘%abc%’
Get the rows where one condition and another condition holds with WHERE condn1 AND condn2
SELECT col1, col2
FROM table
WHERE col1 < value
AND col2 > value2
Get the rows where one condition or another condition holds with WHERE condn1 OR condn2
SELECT col1
FROM table
WHERE col1 < value
OR col2 > value
Get rows where values are missing with WHERE col IS NULL
SELECT col1, col2
FROM table
WHERE col1 IS NULL