dev-resources.site
for different kinds of informations.
π SQL Cheat Sheet for Developers
Published at
8/22/2024
Categories
sql
database
cheatsheet
programming
Author
nullvoidkage
Author
12 person written this
nullvoidkage
open
Hey everyone! π
Iβve compiled a handy SQL Cheat Sheet to help you quickly reference key SQL commands and concepts. Whether youβre working with databases or just need a quick refresher, this guide has got you covered.
Letβs dive in!
π Core Concepts
- SQL: Structured Query Language for managing and manipulating relational databases.
π¦ SQL Commands and Concepts
-
SELECT
: Retrieves data from tables.
SELECT * FROM employees;
-
INSERT
: Adds new records to tables.
INSERT INTO employees (name, position) VALUES ('John Doe', 'Developer');
-
UPDATE
: Modifies existing records in tables.
UPDATE employees SET position = 'Senior Developer' WHERE name = 'John Doe';
-
DELETE
: Removes records from tables.
DELETE FROM employees WHERE name = 'John Doe';
-
WHERE
: Filters records based on conditions.
SELECT * FROM employees WHERE position = 'Developer';
-
JOIN
: Combines records from multiple tables.
SELECT employees.name, departments.department_name
FROM employees
JOIN departments ON employees.department_id = departments.id;
-
INNER JOIN
: Returns matching records from tables.
SELECT * FROM orders INNER JOIN customers ON orders.customer_id = customers.id;
-
LEFT JOIN
: Returns all left table records.
SELECT * FROM employees LEFT JOIN departments ON employees.department_id = departments.id;
-
RIGHT JOIN
: Returns all right table records.
SELECT * FROM employees RIGHT JOIN departments ON employees.department_id = departments.id;
-
FULL JOIN
: Returns all matching/non-matching records.
SELECT * FROM employees FULL JOIN departments ON employees.department_id = departments.id;
-
GROUP BY
: Groups rows sharing common fields.
-
SELECT department_id, COUNT(*) as employee_count
FROM employees
GROUP BY department_id;
-
ORDER BY
: Sorts records in ascending/descending order.
SELECT * FROM employees ORDER BY name ASC;
-
HAVING
: Filters groups after aggregation.
SELECT department_id, COUNT(*) as employee_count
FROM employees
GROUP BY department_id
HAVING COUNT(*) > 5;
-
DISTINCT
: Removes duplicate records from results.
SELECT DISTINCT department_id FROM employees;
-
LIMIT
: Restricts the number of returned records.
SELECT * FROM employees LIMIT 10;
-
OFFSET
: Skips a specific number of records.
SELECT * FROM employees LIMIT 10 OFFSET 20;
-
ALIAS
: Renames tables or columns temporarily.
SELECT name AS employee_name FROM employees;
-
UNION
: Combines results of two SELECTs.
SELECT name FROM employees
UNION
SELECT name FROM contractors;
-
INDEX
: Speeds up data retrieval.
CREATE INDEX idx_employee_name ON employees (name);
-
PRIMARY KEY
: Uniquely identifies each table record.
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(100)
);
-
FOREIGN KEY
: Links records between tables.
CREATE TABLE orders (
id INT PRIMARY KEY,
customer_id INT,
FOREIGN KEY (customer_id) REFERENCES customers(id)
);
-
AUTO_INCREMENT
: Automatically increments numeric values.
CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100)
);
-
NOT NULL
: Ensures column must have a value.
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
-
DEFAULT
: Sets default value for column.
CREATE TABLE employees (
id INT PRIMARY KEY,
status VARCHAR(20) DEFAULT 'active'
);
-
CHECK
: Ensures column meets a condition.
CREATE TABLE employees (
id INT PRIMARY KEY,
age INT CHECK (age >= 18)
);
-
CONSTRAINT
: Defines rules for table data integrity.
ALTER TABLE employees
ADD CONSTRAINT unique_name UNIQUE (name);
-
TRIGGER
: Executes automatic actions on data changes.
CREATE TRIGGER before_employee_insert
BEFORE INSERT ON employees
FOR EACH ROW
BEGIN
SET NEW.created_at = NOW();
END;
-
VIEW
: Virtual table based on a query.
CREATE VIEW employee_view AS
SELECT name, position FROM employees;
-
SUBQUERY
: Nested query within another query.
SELECT name FROM employees
WHERE department_id IN (
SELECT id FROM departments WHERE name = 'Sales'
);
-
TRANSACTION
: Ensures data consistency across operations.
START TRANSACTION;
UPDATE employees SET position = 'Manager' WHERE name = 'John Doe';
COMMIT;
-
COMMIT
: Saves all changes in a transaction.
COMMIT;
-
ROLLBACK
: Undoes changes in a transaction.
ROLLBACK;
-
ACID
: Ensures reliable database transactions (Atomicity, Consistency, Isolation, Durability).
Connect with me:
- LinkedIn: https://www.linkedin.com/in/nikko-ferwelo-358b11213
- GitHub: https://github.com/NullVoidKage
Feel free to reach out or follow me for more content on database management and SQL. Happy querying! π»
cheatsheet Article's
30 articles in total
Vim cheat sheet
read article
The Ultimate Cheat Sheet: CLI Man Pages, tldr, and cheat.sh
read article
From FZF file preview to a browser for cht.sh to discovering the ideal solution
read article
Seaborn Cheat Sheet
read article
SQL Quick Reference: Simplifying Database Management
read article
Terraform Commands Cheat Sheet
read article
JavaScript Interview Cheat Sheet - Part 1
read article
JavaScript Interview Cheat Sheet - Part 2
read article
Arch Linux Pacman: A Detailed Guide with Commands and Examples π©π§
read article
The Art of AI Conversation: 6 Essential Tips for Chat LLM Success
read article
Master CSS Selectors
read article
End-to-End Flexbox vs. Grid vs. Traditional.
read article
Linux Commands Cheat Sheet :)
read article
sql joins: moving in together
read article
A Yocto Cheatsheet
read article
Typescript quick concept refresher and reference
read article
cheat sheet for go mod package management
read article
Git para Iniciantes: Tudo o que vocΓͺ precisa saber para comeΓ§ar a usar
read article
Git Commands You Need for Hacktoberfest 2024 - Git cheat sheet
read article
Git Cheatsheet that will make you a master in Git
read article
How to learn HTML: 46 great sites, courses and books (all free)
read article
Top 5 Cheat sheets for Developers
read article
CSS: List of Properties for Text
read article
What's in a name?
read article
The HTML History and Optimization Cheat Sheet
read article
Kubernetes Cheat Sheet: Essential Commands for Beginners
read article
π¦ GitLab Cheatsheet - 16 - CICD Catalog
read article
π SQL Cheat Sheet for Developers
currently reading
The Ultimate SQL JOIN Cheat Sheet
read article
JavaScript Cheat Sheets
read article
Featured ones: