Master essential SQL commands and data types. This guide covers everything from basic operations and data handling to filtering, functions, operators, and multiple tables, helping you quickly navigate common SQL programming tasks.
Structured Query Language (SQL) is a powerful tool used for managing and manipulating relational databases. It is essential for accessing, updating, and managing the data stored in databases. SQL's popularity stems from its efficiency, versatility, and ease of use, making it a critical skill for data analysts, developers, and anyone working with databases. This cheat sheet provides a quick reference to the most common SQL commands and types, helping you master the basics of SQL programming.
Creates a new database.
CREATE DATABASE my_database;
Selects a database to use.
USE my_database;
Modifies an existing database.
ALTER DATABASE my_database
MODIFY NAME = new_database_name;
Deletes an existing database.
DROP DATABASE my_database;
Creates a new table.
CREATE TABLE employees (
id INT,
name VARCHAR(50),
age INT
);
Creates an index for faster query performance.
CREATE INDEX idx_employee_name
ON employees (name);
Inserts new rows into a table.
INSERT INTO employees (id, name, age)
VALUES (1, 'Alice', 30);
Modifies an existing table.
ALTER TABLE employees
ADD COLUMN department VARCHAR(50);
Deletes an entire table.
DROP TABLE employees;
Retrieves data from a table.
SELECT * FROM employees;
Retrieves unique values from a column.
SELECT DISTINCT department FROM employees;
Limits the number of rows returned by a query.
SELECT * FROM employees
LIMIT 5;
Specifies an offset for the rows returned by a query.
SELECT * FROM employees
LIMIT 5 OFFSET 10;
Retrieves a specific number of rows.
SELECT * FROM employees
FETCH FIRST 5 ROWS ONLY;
Provides conditional logic in a query.
SELECT name,
CASE
WHEN age < 30 THEN 'Young'
ELSE 'Experienced'
END as experience
FROM employees;
Modifies existing rows in a table.
UPDATE employees
SET age = 31
WHERE id = 1;
Sets rules for column values.
ALTER TABLE employees
ADD CONSTRAINT unique_name UNIQUE (name);
Uniquely identifies each row.
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(50)
);
Ensures all values in a column are unique.
ALTER TABLE employees
ADD CONSTRAINT unique_name UNIQUE (name);
Ensures a column cannot have NULL values.
ALTER TABLE employees
MODIFY COLUMN name VARCHAR(50) NOT NULL;
Sets a default value for a column.
ALTER TABLE employees
ADD COLUMN hire_date DATE DEFAULT CURRENT_DATE;
Filters records based on a condition.
SELECT * FROM employees
WHERE age > 30;
Filters records using pattern matching.
SELECT * FROM employees
WHERE name LIKE 'A%';
Filters records that match a list of values.
SELECT * FROM employees
WHERE department IN ('HR', 'IT');
Filters records within a range.
SELECT * FROM employees
WHERE age BETWEEN 25 AND 35;
Filters records with NULL values.
SELECT * FROM employees
WHERE department IS NULL;
Sorts records in ascending or descending order.
SELECT * FROM employees
ORDER BY name ASC;
Combines multiple conditions.
SELECT * FROM employees
WHERE age > 30 AND department = 'IT';
At least one of the conditions must be true.
SELECT * FROM employees
WHERE age > 30 OR department = 'HR';
Excludes specified condition.
SELECT * FROM employees
WHERE NOT department = 'HR';
Searches for a specified pattern.
SELECT * FROM employees
WHERE name LIKE 'A%';
Matches any value in a list.
SELECT * FROM employees
WHERE department IN ('HR', 'Finance');
Matches values within a range.
SELECT * FROM employees
WHERE age BETWEEN 25 AND 35;
Matches NULL values.
SELECT * FROM employees
WHERE department IS NULL;
Sorts the result set.
SELECT * FROM employees
ORDER BY age DESC;
Groups rows sharing a property.
SELECT department, COUNT(*)
FROM employees
GROUP BY department;
Counts the number of rows.
SELECT COUNT(*) FROM employees;
Calculates the sum of a column.
SELECT SUM(salary) FROM employees;
Calculates the average value.
SELECT AVG(age) FROM employees;
Finds the minimum value.
SELECT MIN(age) FROM employees;
Finds the maximum value.
SELECT MAX(age) FROM employees;
Groups rows that have the same values.
SELECT department, COUNT(*)
FROM employees
GROUP BY department;
Filters groups based on a condition.
SELECT department, COUNT(*)
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;
Uniquely identifies each row in a table.
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(50)
);
Uniquely identifies a row in another table.
CREATE TABLE orders (
order_id INT PRIMARY KEY,
employee_id INT,
FOREIGN KEY (employee_id) REFERENCES employees(id)
);
Ensures all values in a column are unique.
ALTER TABLE employees
ADD CONSTRAINT unique_name UNIQUE (name);
Ensures a column cannot have NULL values.
ALTER TABLE employees
MODIFY COLUMN name VARCHAR(50) NOT NULL;
Ensures a column meets a condition.
ALTER TABLE employees
ADD CONSTRAINT check_age CHECK (age >= 18);
Returns rows when there is a match in one of the tables.
SELECT employees.name, orders.order_id
FROM employees
LEFT JOIN orders ON employees.id = orders.employee_id;
Creates a named temporary result set.
WITH department_count AS (
SELECT department, COUNT(*) AS num
FROM employees
GROUP BY department
)
SELECT * FROM department_count;
Combines the result sets of two queries.
SELECT name FROM employees
UNION
SELECT name FROM managers;
Returns the Cartesian product of both tables.
SELECT employees.name, departments.name
FROM employees
CROSS JOIN departments;
Returns rows with a match in both tables.
SELECT employees.name, departments.name
FROM employees
INNER JOIN departments ON employees.department_id = departments.id;
Calculates average value.
SELECT AVG(salary) FROM employees;
Concatenates two or more strings.
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM employees;
SELECT SUBSTR(name, 1, 3) AS short_name FROM employees;
SELECT INSERT(name, 1, 0, 'Dr. ') AS titled_name FROM employees;
SELECT CURRENT_DATE;
Calculates the square root of a number.
SELECT SQRT(salary) FROM employees;
This cheat sheet provides a concise overview of essential SQL commands and their usage, helping you quickly reference and master the basics of SQL programming. Whether you are a beginner or looking to refresh your knowledge, Coursera offers online courses in SQL to support your career journey and goals.
Writer
Coursera is the global online learning platform that offers anyone, anywhere access to online course...
This content has been made available for informational purposes only. Learners are advised to conduct additional research to ensure that courses and other credentials pursued meet their personal, professional, and financial goals.