Study smarter with Fiveable
Get study guides, practice questions, and cheatsheets for all your subjects. Join 500,000+ students with a 96% pass rate.
SQL commands form the backbone of every database interaction you'll encounter in this course and beyond. Whether you're building a web application, analyzing business data, or designing enterprise systems, you're being tested on your ability to retrieve, manipulate, and structure data efficiently. These commands aren't just syntax to memorize—they represent fundamental operations that map directly to relational algebra concepts, data integrity principles, and query optimization strategies.
Understanding SQL commands means recognizing when and why to use each one. Exams will test whether you can write correct queries, but FRQs often dig deeper: Can you explain why a JOIN outperforms a subquery? Do you know the difference between WHERE and HAVING? Don't just memorize the syntax—know what category each command belongs to and what problem it solves.
These commands handle the day-to-day operations of working with data—the CRUD operations (Create, Read, Update, Delete) that form the foundation of application development.
* for all columns, though explicit column lists improve performance and clarity$$INSERT INTO table (col1, col2) VALUES (val1, val2)$$Compare: UPDATE vs. DELETE—both require WHERE clauses to target specific rows, both can affect all records if WHERE is omitted. The difference: UPDATE modifies values in place, DELETE removes entire rows. If an FRQ asks about data integrity risks, mention the missing WHERE clause problem for both.
DDL commands define the schema—the blueprint of your database. These commands create, modify, and remove database objects like tables, indexes, and views. Changes are typically auto-committed and cannot be rolled back.
$$ALTER TABLE users ADD COLUMN email VARCHAR(255)$$Compare: CREATE TABLE vs. ALTER TABLE—both modify schema, but CREATE builds from scratch while ALTER modifies in place. CREATE INDEX vs. PRIMARY KEY—both can enforce uniqueness, but indexes are optional performance tools while primary keys are mandatory identifiers.
These clauses modify how SELECT statements process and return data. They execute in a specific order: FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY.
$$=$$, $$<$$, $$>$$, $$<=$$, $$>=$$, $$<>$$, plus logical operators AND, OR, NOT$$ORDER BY department ASC, salary DESC$$$$HAVING COUNT(*) > 5$$ which WHERE cannot evaluateCompare: WHERE vs. HAVING—both filter data, but WHERE filters individual rows before grouping while HAVING filters groups after aggregation. Classic exam question: "Show departments with more than 10 employees"—you need GROUP BY department with HAVING COUNT(*) > 10, not WHERE.
These commands work across multiple tables or result sets, enabling the relational power of relational databases. Understanding these is essential for normalized database design.
Compare: JOIN vs. UNION—both combine data from multiple sources, but JOIN combines columns horizontally (adding more fields) while UNION combines rows vertically (adding more records). FRQ tip: If asked to "combine customer data from two regional tables," that's UNION. If asked to "show customers with their orders," that's JOIN.
These commands create persistent database objects that simplify complex operations and provide abstraction layers over raw tables.
Compare: CREATE VIEW vs. CREATE TABLE—both create queryable objects, but tables store actual data while views store query definitions. Views always reflect current underlying data; tables require explicit updates. Use views for reporting and access control, tables for persistent storage.
| Concept | Best Examples |
|---|---|
| Data Retrieval | SELECT, WHERE, ORDER BY |
| Data Modification | INSERT, UPDATE, DELETE |
| Schema Definition | CREATE TABLE, ALTER TABLE, DROP TABLE |
| Aggregation & Grouping | GROUP BY, HAVING, aggregate functions |
| Multi-Table Queries | JOIN, UNION |
| Performance Optimization | CREATE INDEX |
| Abstraction & Security | CREATE VIEW |
| Row Filtering | WHERE (before grouping), HAVING (after grouping) |
Which two commands both require a WHERE clause to avoid affecting all rows in a table? What happens if you omit it in each case?
Compare WHERE and HAVING: If you need to find all departments where the average salary exceeds $50,000, which clause would you use and why?
What's the fundamental difference between JOIN and UNION? Give a scenario where each would be the appropriate choice.
CREATE INDEX improves SELECT performance but has a trade-off. What operations become slower when you add indexes, and why?
FRQ-style: You're designing a database for a library system. Explain when you would use CREATE TABLE vs. CREATE VIEW, and provide an example of each that demonstrates their different purposes.