Some of the fundamental SQL commands used to interact with relational databases.
SELECT Statement
The cornerstone of data retrieval in SQL databases.
Key Topics:
-
Basic Syntax Structure:
SELECT column1, column2 FROM table_name WHERE condition;
-
Column Selection Techniques:
- Selecting specific columns vs using wildcards (
SELECT *
)
- Column aliasing with
AS
keyword
- Handling NULL values with
COALESCE
and IFNULL
functions
-
Data Filtering with WHERE Clause:
- Comparison operators (
=
, <>
, <
, >
, <=
, >=
)
- Boolean operators (
AND
, OR
, NOT
)
- Pattern matching with
LIKE
and wildcards (%
, _
)
- Range checking with
BETWEEN
and IN
-
Result Organization:
- Sorting with
ORDER BY
(ascending/descending)
- Grouping with
GROUP BY
and aggregate functions (COUNT
, SUM
, AVG
, MIN
, MAX
)
- Filtering grouped results with
HAVING
clause
- Limiting results with
LIMIT
/OFFSET
or TOP
/ROWNUM
-
Joining Tables:
- INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN
- Self-joins and cross joins
- Using table aliases for cleaner syntax
-
Subqueries and Common Table Expressions (CTEs)
- Using subqueries in SELECT, WHERE, HAVING clauses
- Using CTEs with the
WITH
clause for more readable complex queries
INSERT Statement
Adding new records to database tables.
Key Topics:
-
Single Row Insertion:
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
-
Multiple Row Insertion:
INSERT INTO table_name (column1, column2)
VALUES (value1, value2), (value3, value4), (value5, value6);
-
Insertion with Subqueries:
INSERT INTO target_table (column1, column2)
SELECT column_a, column_b FROM source_table WHERE condition;
-
Default and Auto-generated Values:
- Working with identity columns and sequences
- Using DEFAULT keyword for default values
- NULL handling during insertion
-
Handling Constraint Violations:
- Managing primary key, unique, foreign key, and check constraints
- Using IGNORE or ON DUPLICATE KEY options (MySQL)
- MERGE/UPSERT operations (Oracle, SQL Server)
-
Bulk Insert Operations:
- Optimizing for large data sets
- Using database-specific bulk loaders
- Transaction management for batch inserts
UPDATE Statement
Modifying existing records in database tables.
Key Topics:
-
Basic Syntax:
UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;
-
Update Techniques:
- Computing values based on existing data (
SET salary = salary * 1.1
)
- Using functions in updates
- Multi-column updates
-
Conditional Updates:
- WHERE clause best practices
- Using CASE expressions for complex conditional logic
- Using subqueries in conditions
-
Mass Updates vs. Targeted Updates:
- Performance considerations
- Transaction isolation levels
- Locking implications
-
Updating with Joins:
UPDATE table1 t1
JOIN table2 t2 ON t1.id = t2.id
SET t1.column = t2.column
WHERE condition;
-
Safe Update Practices:
- Using transactions for rollback capability
- Testing updates with SELECT first
- Using row count verification
DELETE Statement
Removing records from database tables.
Key Topics:
-
Basic Syntax:
DELETE FROM table_name WHERE condition;
-
Safe Deletion Practices:
- Using transactions and savepoints
- Testing with SELECT before executing DELETE
- Creating deletion scripts with built-in verification
-
Targeted vs. Mass Deletion:
- Performance implications
- Truncate vs. Delete operations
- Logging and auditing requirements
-
Cascading Deletes:
- Foreign key constraints with CASCADE options
- Managing parent-child relationships
- Orphaned record handling
-
Multi-table Deletes:
DELETE t1 FROM table1 t1
JOIN table2 t2 ON t1.id = t2.id
WHERE condition;
-
Soft Deletes vs. Hard Deletes:
- Using status flags or timestamps instead of physical deletion
- Archiving strategies for historical data
- Recovery mechanisms for accidental deletions
Best Practices Across All Operations
- Transaction Management: Using BEGIN, COMMIT, and ROLLBACK appropriately
- Performance Optimization: Indexing strategies for each operation type
- Security Considerations: Parameterized queries to prevent SQL injection
- Error Handling: Proper error catching and reporting mechanisms
- Database Engine Variations: Syntax differences between major database systems (MySQL, PostgreSQL, SQL Server, Oracle)
Last modified: Thursday, 10 April 2025, 3:57 PM