mySQL Flashcards
Introduction
Get all columns from a table in MySQL
SELECT *
FROM table
Get a column from a table in MySQL
SELECT column
FROM table
Get multiple columns from table in MySQL
SELECT col1, col2
FROM table
in MySQL Use alias names
AS
SELECT col1, col2 AS col2_new
FROM table
Arrange the rows in ascending order of values in by a specific column in MySQL
ORDER BY
SELECT col1, col2
FROM table
ORDER BY col2
Arrange the rows in descending order of values in column (in MySQL)
ORDER BY col DESC
SELECT col1, col2
FROM table
ORDER BY col2 DESC
Limit the number of rows returned (in MySQL)
LIMIT
SELECT *
FROM table
LIMIT 2
Get unique values, filtering out duplicate rows, returning only unique rows. (in MySQL)
DISTINCT
SELECT DISTINCT column
FROM table
Get rows where a number is greater than a value (in MySQL)
WHERE col1 > n
SELECT col1
FROM table
WHERE col1 > value
Get rows where a number is greater than or equal to a value (in MySQL)
WHERE col >=n
SELECT col1
FROM table
WHERE col1 >= value
Visualize Concatenating Columns with a New Name (in MySQL)
SELECT CONCAT(first_name, ‘ ‘, last_name) AS full_name
FROM employees;
Visualize Using in Aggregations (in MySQL)
SELECT department, SUM(salary) AS total_salary
FROM employees
GROUP BY department;
Get rows where a number is equal to a value (in MySQL)
SELECT col1
FROM table
WHERE col1 = value
Get rows where a number is not equal ( WHERE col != n) (in MySQL)
SELECT col1
FROM table
WHERE col1 <> value
Get rows where a number is between two values (inclusive) (in MySQL)
SELECT col1
FROM table
WHERE col1 BETWEEN value1 AND value2
Get rows where text is equal to a value (in MySQL)
SELECT col1, col2
FROM table
WHERE col1 = ‘string’
Get rows where text is one of several values (in MySQL)
SELECT col1, col2
FROM table
WHERE x IN (‘string’, ‘string2’)
(in MySQL) 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 (in MySQL)
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 (in MySQL)
SELECT col1
FROM table
WHERE col1 < value
OR col2 > value
Get rows where values are missing with WHERE col IS NULL (in MySQL)
SELECT col1, col2
FROM table
WHERE col1 IS NULL
Get rows where values are not missing with WHERE col IS NOT NULL (in MySQL)
SELECT col1, col2
FROM table
WHERE col1 IS NOT NULL
Get the total number of rows (in MySQL)
SELECT COUNT(*)
FROM table
Get the total value of a column (in MySQL)
SELECT SUM(col)
FROM table