<aside> <img src="/icons/code_orange.svg" alt="/icons/code_orange.svg" width="40px" /> Fundamental SQL Syntax

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

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

-- Filtering rows with WHERE clause
SELECT * FROM table_name WHERE condition;

-- Sorting results with ORDER BY
SELECT * FROM table_name ORDER BY column1 ASC;  -- ASC for ascending, DESC for descending

<aside> <img src="/icons/code_orange.svg" alt="/icons/code_orange.svg" width="40px" /> Data Querying

-- Basic SELECT statement
SELECT column1, column2 FROM table_name;

-- Using WHERE clause for filtering
SELECT * FROM table_name WHERE condition;

-- Aggregate functions: COUNT, SUM, AVG, MAX, MIN
SELECT COUNT(column_name) FROM table_name;
SELECT SUM(column_name) FROM table_name;

-- GROUP BY clause
SELECT column_name, COUNT(*) FROM table_name
GROUP BY column_name;

-- HAVING clause (used with GROUP BY)
SELECT column_name, COUNT(*) FROM table_name
GROUP BY column_name
HAVING COUNT(*) > value;

-- JOIN operations
SELECT columns
FROM table1
JOIN table2 ON table1.column = table2.column;

<aside> <img src="/icons/code_orange.svg" alt="/icons/code_orange.svg" width="40px" /> Data Manipulation Language (DML)

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

-- Update existing data
UPDATE table_name
SET column1 = value1, column2 = value2
WHERE condition;

-- Delete data from a table
DELETE FROM table_name
WHERE condition;

<aside> <img src="/icons/code_orange.svg" alt="/icons/code_orange.svg" width="40px" /> Transactions

-- Start a transaction
BEGIN TRANSACTION;

-- Commit a transaction
COMMIT;

-- Rollback a transaction
ROLLBACK;

<aside> <img src="/icons/code_orange.svg" alt="/icons/code_orange.svg" width="40px" /> Common Error Handling

-- Handling NULL values
SELECT column_name FROM table_name
WHERE column_name IS NULL;

-- Avoiding SQL injection (parameterised queries)
SELECT column_name FROM table_name
WHERE column_name = ?;

<aside> <img src="/icons/code_orange.svg" alt="/icons/code_orange.svg" width="40px" /> Data Definition Language (DDL)

-- Create a new table
CREATE TABLE table_name (
    column1 datatype,
    column2 datatype,
    ...
);

-- Alter an existing table (add a column)
ALTER TABLE table_name
ADD column_name datatype;

-- Drop a table
DROP TABLE table_name;

<aside> <img src="/icons/code_orange.svg" alt="/icons/code_orange.svg" width="40px" /> Advanced Queries

-- Subqueries
SELECT column1 FROM table_name
WHERE column2 = (SELECT column3 FROM another_table WHERE condition);

-- UNION operation
SELECT column1 FROM table_name1
UNION
SELECT column1 FROM table_name2;

-- CASE statement for conditional logic
SELECT column1,
CASE
    WHEN condition1 THEN result1
    WHEN condition2 THEN result2
    ELSE result3
END AS alias_name
FROM table_name;

<aside> <img src="/icons/code_orange.svg" alt="/icons/code_orange.svg" width="40px" /> Useful SQL Functions

-- String functions
SELECT UPPER(column_name) FROM table_name;
SELECT LOWER(column_name) FROM table_name;
SELECT LENGTH(column_name) FROM table_name;

-- Date functions
SELECT CURRENT_DATE;
SELECT CURRENT_TIME;
SELECT EXTRACT(YEAR FROM date_column) FROM table_name;

-- Numeric functions
SELECT ROUND(column_name, decimals) FROM table_name;
SELECT ABS(column_name) FROM table_name;

<aside> <img src="/icons/code_orange.svg" alt="/icons/code_orange.svg" width="40px" /> Indexes and Keys

-- Create an index
CREATE INDEX index_name ON table_name (column_name);

-- Create a unique index
CREATE UNIQUE INDEX index_name ON table_name (column_name);

-- Add a primary key
ALTER TABLE table_name
ADD PRIMARY KEY (column_name);

-- Add a foreign key
ALTER TABLE table_name
ADD CONSTRAINT fk_name
FOREIGN KEY (column_name) REFERENCES other_table(column_name);

<aside> <img src="/icons/code_orange.svg" alt="/icons/code_orange.svg" width="40px" /> Common SQL Clauses

-- DISTINCT clause
SELECT DISTINCT column_name FROM table_name;

-- LIMIT clause
SELECT * FROM table_name LIMIT number_of_rows;

-- OFFSET clause
SELECT * FROM table_name LIMIT number_of_rows OFFSET offset_value;

<aside> <img src="/icons/code_orange.svg" alt="/icons/code_orange.svg" width="40px" /> SQL Best Practices

Explanation: Well-named tables and columns reduce the cognitive load on developers and analysts, making it easier to understand the data structure and intent behind each query.

Explanation: Normalization involves organizing your data so that redundancy is minimized, typically by splitting large tables into smaller, related tables. This leads to more efficient updates and queries.

Explanation: Indexes make data retrieval faster, but they also consume space and can slow down write operations. Knowing when and where to use indexes is key to maintaining a balanced database.

Explanation: SELECT * can lead to inefficient queries, especially in large tables. By specifying only the columns you need, you reduce the amount of data processed and transferred.

Explanation: Readable queries are easier to debug and maintain. Using subqueries and comments helps to break down complex logic, making it easier for others (and your future self) to understand.

Professional Practices: Following these best practices is essential for writing efficient, maintainable SQL code. In the full course, you’ll learn advanced techniques for query optimization, indexing strategies, and database design.

</aside>

UPDATE: Get the SQL Masterclass [ Premium ] Template!