🔹 DELETE vs TRUNCATE vs DROP
- DELETE: Removes specific rows, can be rolled back.
DELETE FROM table_name WHERE condition;
- TRUNCATE: Removes all rows, resets indexes, cannot be rolled back.
TRUNCATE TABLE table_name;
- DROP: Deletes table structure & data permanently.
DROP TABLE table_name;
🔹 WHERE vs HAVING
- WHERE: Filters before aggregation.
SELECT * FROM table_name WHERE condition;
- HAVING: Filters after aggregation.
SELECT column, COUNT(<em>) FROM table_name GROUP BY column HAVING COUNT(</em>) > 1;
🔹 INNER vs LEFT vs RIGHT vs FULL JOIN
– INNER JOIN: Only matching records.
SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id;
– LEFT JOIN: All from left + matching right.
SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id;
– RIGHT JOIN: All from right + matching left.
SELECT * FROM table1 RIGHT JOIN table2 ON table1.id = table2.id;
– FULL JOIN: All records from both tables.
SELECT * FROM table1 FULL JOIN table2 ON table1.id = table2.id;
🔹 Primary Key vs Unique Key
- Primary Key: Unique + NOT NULL (One per table).
CREATE TABLE table_name (id INT PRIMARY KEY, name VARCHAR(255));
- Unique Key: Only unique, allows NULL values.
CREATE TABLE table_name (id INT UNIQUE, name VARCHAR(255));
What Undercode Say
SQL is a powerful language for managing and manipulating relational databases. Understanding the differences between commands like DELETE, TRUNCATE, and DROP is crucial for database management. DELETE allows for row-specific removal with rollback capabilities, while TRUNCATE clears all rows and resets indexes, and DROP eliminates the entire table structure.
The distinction between WHERE and HAVING is vital for filtering data. WHERE is used before aggregation, whereas HAVING filters results after aggregation, making it essential for grouped queries.
Joins are fundamental for combining data from multiple tables. INNER JOIN retrieves only matching records, while LEFT JOIN and RIGHT JOIN include all records from one table and matching records from the other. FULL JOIN combines all records from both tables, ensuring no data is left out.
Primary and Unique Keys enforce data integrity. A Primary Key ensures unique and non-null values, while a Unique Key allows for uniqueness with optional NULL values.
For further reading, check out these resources:
Practice these commands to master SQL:
-- Create a table CREATE TABLE employees (id INT PRIMARY KEY, name VARCHAR(255), department VARCHAR(255)); -- Insert data INSERT INTO employees (id, name, department) VALUES (1, 'John Doe', 'HR'); -- Update data UPDATE employees SET department = 'IT' WHERE id = 1; -- Delete data DELETE FROM employees WHERE id = 1; -- Select with JOIN SELECT employees.name, departments.department_name FROM employees INNER JOIN departments ON employees.department_id = departments.id;
Mastering SQL commands and concepts is essential for database management, data analysis, and backend development. Keep practicing and exploring advanced SQL features to enhance your skills.
References:
Hackers Feeds, Undercode AI