← Snippets

SQL Essentials

 -- Select all records from a table
SELECT * FROM table_name;

-- Select specific columns from a table
SELECT column1, column2 FROM table_name;

-- Select records with a condition
SELECT * FROM table_name WHERE column1 = value1;

-- Select records with multiple conditions using AND
SELECT * FROM table_name WHERE column1 = value1 AND column2 = value2;

-- Select records with multiple conditions using OR
SELECT * FROM table_name WHERE column1 = value1 OR column2 = value2;

-- Select records sorted by a column in ascending order
SELECT * FROM table_name ORDER BY column1 ASC;

-- Select records sorted by a column in descending order
SELECT * FROM table_name ORDER BY column1 DESC;

-- Insert a new record into a table
INSERT INTO table_name (column1, column2) VALUES (value1, value2);

-- Update existing record in a table
UPDATE table_name SET column1 = value1, column2 = value2 WHERE id = value;

-- Delete record from a table
DELETE FROM table_name WHERE id = value;

-- Count the number of records in a table
SELECT COUNT(*) FROM table_name;

-- Find the minimum value in a column
SELECT MIN(column1) FROM table_name;

-- Find the maximum value in a column
SELECT MAX(column1) FROM table_name;

-- Calculate the average value in a column
SELECT AVG(column1) FROM table_name;

-- Calculate the sum of values in a column
SELECT SUM(column1) FROM table_name;

-- Select distinct values from a column
SELECT DISTINCT column1 FROM table_name;

-- Join two tables
SELECT * FROM table1 INNER JOIN table2 ON table1.column1 = table2.column2;

-- Left join two tables
SELECT * FROM table1 LEFT JOIN table2 ON table1.column1 = table2.column2;

-- Right join two tables
SELECT * FROM table1 RIGHT JOIN table2 ON table1.column1 = table2.column2;

-- Full join two tables
SELECT * FROM table1 FULL OUTER JOIN table2 ON table1.column1 = table2.column2;

-- Group records by a column
SELECT column1, COUNT(*) FROM table_name GROUP BY column1;

-- Filter groups by a condition
SELECT column1, COUNT(*) FROM table_name GROUP BY column1 HAVING COUNT(*) > value;