Basic Queries - SQL 50 Study Guide
Master the fundamentals of SQL data retrieval and filtering through 8 essential problems.
Table of Contents
- Select All Employees
- Find High Salary Employees
- List Unique Departments
- Sort Employees by Salary
- Find Employees with NULL Values
- Pattern Matching with Names
- Salary Range Analysis
- Calculate Employee Metrics
1. Recyclable and Low Fat Products (LC 1757)
🔗 LeetCode Link: Recyclable and Low Fat Products - LeetCode #1757
📊 Schema First (Active Analysis)
Before writing any SQL, spend 2-3 minutes analyzing the table structure:
Schema Analysis Questions:
- What information is stored about each employee?
- Which column would uniquely identify each employee?
- What might be the business purpose of each column?
Take time to understand the data model before continuing…
Table Structure:
CREATE TABLE Products (
product_id INT PRIMARY KEY,
low_fats ENUM('Y','N'),
recyclable ENUM('Y','N')
);
-- Sample Data
INSERT INTO Products VALUES
(0, 'Y', 'N'),
(1, 'Y', 'Y'),
(2, 'N', 'Y'),
(3, 'Y', 'Y'),
(4, 'N', 'N');
💡 Query Construction (Guided Learning)
Step 1: Understanding Data Requirements
Guided Question: What does “select all employees” mean in terms of columns and rows?
💭 Think about it, then click to reveal
"Select all employees" typically means: - **Columns**: All available columns for each employee - **Rows**: Every employee record in the table - **No filtering**: We want the complete dataset This is the most basic SELECT operation in SQL.Step 2: Basic SELECT Syntax
Guided Question: What SQL keywords do you need to retrieve all data from a table?
💭 Think about it, then click to reveal
The basic syntax is: ```sql SELECT column_list FROM table_name; ``` For all columns, we can use: - `SELECT *` (asterisk means "all columns") - Or list each column explicitly: `SELECT col1, col2, col3` The `*` is convenient but explicit column lists are often better for production code.Step 3: Best Practices Consideration
Guided Question: When might you avoid using SELECT * in real applications?
💭 Think about it, then click to reveal
Avoid `SELECT *` when: - **Performance**: You only need specific columns - **Maintainability**: Table structure might change - **Security**: Some columns might contain sensitive data - **Network**: Transferring unnecessary data Explicit column selection is generally preferred in production applications.Solutions
Approach 1: Basic AND Condition
-- Find products that are both low fat and recyclable
SELECT product_id
FROM Products
WHERE low_fats = 'Y' AND recyclable = 'Y';
Explanation:
WHERE
clause filters rows based on conditionsAND
operator requires both conditions to be true- Returns only products meeting both criteria
- Simple filtering using equality comparison
Approach 2: Explicit Column Selection
-- Show all information for qualifying products
SELECT product_id, low_fats, recyclable
FROM Products
WHERE low_fats = 'Y' AND recyclable = 'Y';
Trade-offs:
- Explicit: More verbose but clearer intent
- Wildcard: Shorter but less specific
- Performance: Minimal difference for small tables
- Maintainability: Explicit is more robust to schema changes
🎯 Query Practice & Self-Assessment
Implementation Challenge
Try writing both queries from memory:
Step-by-step checklist:
- Write a SELECT statement using *
- Write a SELECT statement with explicit columns
- Identify the table name correctly
- Ensure proper SQL syntax (semicolon, etc.)
Schema Variations
Practice adapting your solution:
- What if you only wanted first_name and salary?
- How would you select all columns from a table named “Staff”?
- What happens if you misspell a column name?
Reflection Questions
After solving, think about:
- Query Logic: Can you explain the difference between * and explicit columns?
- Performance: Which approach would be better for a table with 50 columns?
- Alternative Approaches: Are there other ways to see table data?
- Pattern Recognition: What other problems require “select all” operations?
Confidence Rating
Rate your confidence (1-5) on:
- Understanding basic SELECT syntax: ___/5
- Writing simple queries: ___/5
- Choosing between * and explicit columns: ___/5
- Explaining the approach: ___/5
Next Steps
- If confidence is 3+ on all: Move to next problem
- If confidence is <3: Practice more basic SELECT variations
- Try selecting specific columns to build familiarity
2. Find Customer Referee (LC 584)
🔗 LeetCode Link: Find Customer Referee - LeetCode #584
📊 Schema First (Active Analysis)
Using the same Employees table from Problem 1:
Schema Analysis Questions:
- Which column contains salary information?
- What would define “high salary” in this context?
- How would you filter rows based on a condition?
💡 Query Construction (Guided Learning)
Step 1: Understanding Filtering Requirements
Guided Question: How do you limit query results to only rows that meet certain criteria?
💭 Think about it, then click to reveal
SQL uses the `WHERE` clause to filter rows: - Comes after the `FROM` clause - Contains conditions that evaluate to true/false - Only rows where condition is true are returned For salary filtering, we need to compare the salary column to a threshold value.Step 2: Comparison Operators
Guided Question: What SQL operator would you use to find salaries greater than a specific amount?
💭 Think about it, then click to reveal
Common comparison operators: - `>` greater than - `>=` greater than or equal to - `<` less than - `<=` less than or equal to - `=` equal to - `!=` or `<>` not equal to For "high salary" (let's say > 70000), we'd use: `WHERE salary > 70000`Step 3: Result Set Considerations
Guided Question: What columns should you return for a “high salary employees” query?
💭 Think about it, then click to reveal
Consider what information would be useful: - **Basic**: Employee name and salary - **Complete**: All employee information for context - **Analytical**: Maybe just names for a simple list The choice depends on the business requirement and how the data will be used.Solutions
Approach 1: Basic High Salary Filter
-- Find employees earning more than $70,000
SELECT first_name, last_name, salary
FROM Employees
WHERE salary > 70000;
Approach 2: Complete Information with Sorting
-- Get all information for high earners, sorted by salary
SELECT *
FROM Employees
WHERE salary > 70000
ORDER BY salary DESC;
🎯 Query Practice & Self-Assessment
Implementation Challenge
Step-by-step checklist:
- Add a WHERE clause after FROM
- Use the correct comparison operator
- Choose appropriate columns to display
- Consider adding ORDER BY for better results
3. List Unique Departments
🔗 LeetCode Link: Find Duplicate Emails - LeetCode #182
📊 Schema First (Active Analysis)
Schema Analysis Questions:
- Which column contains department information?
- Why might there be duplicate department values?
- What does “unique” mean in this context?
💡 Query Construction (Guided Learning)
Step 1: Understanding Duplicates
Guided Question: Why would a departments column have repeated values?
💭 Think about it, then click to reveal
Multiple employees can work in the same department: - Each employee row has their department - Department names repeat for each employee in that department - We want to see each department name only once This is where `DISTINCT` becomes useful.Step 2: DISTINCT Keyword
Guided Question: How does SQL eliminate duplicate values from results?
💭 Think about it, then click to reveal
The `DISTINCT` keyword: - Goes immediately after `SELECT` - Removes duplicate rows from the result set - Works on the entire row or specified columns - Syntax: `SELECT DISTINCT column_name FROM table_name`Step 3: Sorting Considerations
Guided Question: How could you make the unique departments list more readable?
💭 Think about it, then click to reveal
Add sorting with `ORDER BY`: - Alphabetical order is usually most readable - `ORDER BY department ASC` (ascending, A-Z) - `ORDER BY department DESC` (descending, Z-A) - ASC is default, so `ORDER BY department` works tooSolutions
Approach 1: Simple Distinct
-- Get unique department names
SELECT DISTINCT department
FROM Employees;
Approach 2: Sorted Unique Departments
-- Get unique departments in alphabetical order
SELECT DISTINCT department
FROM Employees
ORDER BY department;
Approach 3: With Count Information
-- Show departments with employee count
SELECT department, COUNT(*) as employee_count
FROM Employees
GROUP BY department
ORDER BY department;
🎯 Query Practice & Self-Assessment
Implementation Challenge
Step-by-step checklist:
- Use DISTINCT keyword correctly
- Select the appropriate column
- Add sorting for readability
- Compare results with and without DISTINCT
Continue with remaining problems following the same enhanced learning format…
Quick Reference
SQL Keywords Covered
SELECT
- Choose columns to retrieveFROM
- Specify source tableWHERE
- Filter rows by conditionsDISTINCT
- Remove duplicatesORDER BY
- Sort results*
- All columns wildcard
Common Patterns
- Basic Retrieval:
SELECT columns FROM table
- Filtering:
SELECT columns FROM table WHERE condition
- Unique Values:
SELECT DISTINCT column FROM table
- Sorting:
SELECT columns FROM table ORDER BY column
Next Steps
After mastering basic queries, proceed to Sorting & Aggregation Guide to learn GROUP BY and aggregate functions.