16 SQL Operations
You have learned that a SQL query uses SELECT, FROM, and WHERE to specify the data to be returned from the query.
This section expands on how to format queries, use WHERE conditions, select all columns in a table, add comments, and apply aliases — all essential techniques that make your SQL queries more readable, efficient, and professional.
Finally, you’ll see a real-world example of how a data analyst might use SQL to pull employee data for a project.
Capitalization, Indentation, and Semicolons
You can write SQL queries in lowercase, and extra spaces generally don’t matter.
However, using capitalization and indentation improves readability and helps when troubleshooting queries later.
SELECT field1 FROM table WHERE field1 = condition;
The semicolon (;) at the end is a statement terminator — part of the ANSI SQL-92 standard.
Some databases don’t require it, but including it is a good practice for consistency.
WHERE Conditions
The WHERE clause filters data by returning only rows that meet specific conditions.
Example 1 — exact match:
WHERE field1 = 'Chavez'
Example 2 — pattern matching with LIKE:
WHERE field1 LIKE 'Ch%'
Here, % is a wildcard that matches one or more characters.
So, Chavez and Chen would both be returned.
Note: Some databases use
*as the wildcard instead of%.
SELECT All Columns
You can select all columns in a table using SELECT *.
SELECT * FROM table;
While correct syntactically, it’s best to use * sparingly, especially for large tables — selecting too much data can slow down queries.
Adding Comments
When tables or columns have unclear names, comments can help clarify their purpose.
Comments are text ignored by the database but visible to you and others reading the code.
Example (inline and block comments):
SELECT field1 /* this is the last name column */ FROM table -- this is the customer data table WHERE field1 LIKE 'Ch%';
You can also add comments before or within queries:
-- This query joins with the accounts table SELECT rowkey, -- key used to join with account_id Info.date, -- date in format YYYY-MM-DD HH:MM:SS Info.code -- e.g., 'pub-###' FROM Publishers;
Use
--for single-line comments (most common) or/* ... */for block comments if your database supports them.
Adding clear comments helps your future self — and your teammates — understand complex queries months or even years later.
Example: Query with Comments
Example in BigQuery:
-- Pull basic information from the customer table SELECT customer_id, -- main ID used to join with customer_address first_name, -- customer's first name from loyalty program last_name -- customer's last name FROM customer_data.customer_name;
Two dashes (--) are supported in most SQL databases.
You can also use # for comments, but note that MySQL doesn’t recognize #.
Consistency is key — pick a commenting style and use it throughout your work.
Aliases
Aliases let you assign shorter or more descriptive names to tables or columns using the AS keyword.
They make your SQL easier to read and write without changing the actual database schema.
SELECT my_table_alias.actual_column_name AS my_column_alias FROM actual_table_name AS my_table_alias;
These aliases exist only for the duration of the query.
Putting SQL to Work as a Data Analyst
Imagine you’re a data analyst asked to retrieve employee information for a small business.
Step 1: Select all columns
SELECT * FROM Employee;
This retrieves every column in the Employee table.
Step 2: Add a filter with WHERE
SELECT * FROM Employee WHERE jobCode = 'SFI';
Sample output:
| empID | firstName | lastName | jobCode | salary |
|---|---|---|---|---|
| 0002 | Homer | Simpson | SFI | 15000 |
| 0003 | Marge | Simpson | SFI | 30000 |
| 0034 | Bart | Simpson | SFI | 25000 |
| 0067 | Lisa | Simpson | SFI | 38000 |
| 0088 | Ned | Flanders | SFI | 42000 |
| 0076 | Barney | Gumble | SFI | 32000 |
Step 3: Combine conditions with AND and <> (not equal)
SELECT * FROM Employee WHERE jobCode <> 'INT' AND salary <= 30000;
This excludes interns (INT) while selecting only full-time employees earning ≤ $30,000.
Result:
| empID | firstName | lastName | jobCode | salary |
|---|---|---|---|---|
| 0002 | Homer | Simpson | SFI | 15000 |
| 0003 | Marge | Simpson | SFI | 30000 |
| 0034 | Bart | Simpson | SFI | 25000 |
| 0108 | Edna | Krabappel | TUL | 18000 |
| 0099 | Moe | Szyslak | ANA | 28000 |
This data can help your manager identify employees who might need salary adjustments — improving equity, satisfaction, and retention.
Resources to Learn More
- W3Schools SQL Tutorial — Interactive guide to learn SQL syntax and examples.
- SQL Cheat Sheet — Quick reference for SQL functions and operators.
Key Takeaways
- SQL queries use
SELECT,FROM, andWHEREto extract data efficiently.
- Formatting with capitalization, indentation, and semicolons enhances readability.
- Comments clarify purpose and improve collaboration.
- Aliases make complex queries easier to work with.
- SQL empowers analysts to quickly retrieve, analyze, and interpret data — driving smarter business decisions.