SQL Fundamentals
Master the core building blocks: SELECT, WHERE filters, ORDER BY, DISTINCT, LIMIT, and table aliases.
Core Learning Objectives
- •Filter records precisely with WHERE and boolean operators (AND, OR, NOT, BETWEEN, IN)
- •Sort datasets ascending/descending with NULLS FIRST / LAST
- •Eliminate duplicates and write aliased column expressions
Interactive Cheatsheet
PostgreSQL SyntaxSELECT DISTINCT column1, column2 AS alias_name FROM table_name WHERE status = 'active' AND created_at >= '2026-01-01' ORDER BY created_at DESC LIMIT 10;
Curated Practice Problems (5)
Teams Meeting Duration Outliers
Microsoft's Modern Workplace analytics team is auditing Teams meetings. Write a query to identify all meetings that lasted longer than 120 minutes (2 hours). Output: - `meeting_id` - `organizer_id` - `duration_minutes` Order the results by `duration_minutes` descending. Table: - `teams_meetings` (`meeting_id` INT, `organizer_id` INT, `start_time` TIMESTAMP, `end_time` TIMESTAMP)
Low Stock Inventory Alert & Restock Priority
Walmart's fulfillment center needs an automated alert system for products whose current inventory cannot cover the next 7 days of estimated demand. Given: - `daily_sales_rate` is the average number of units sold per day. - A product is at risk if `current_inventory < (daily_sales_rate * 7)`. Write a query to return: - `product_id` - `product_name` - `current_inventory` - `units_shortage` (defined as `(daily_sales_rate * 7) - current_inventory`) Sort the output by `units_shortage` descending. Table: - `inventory` (`product_id` INT, `product_name` VARCHAR, `current_inventory` INT, `daily_sales_rate` INT)
Duplicate ASIN Product Listings
Amazon Marketplace needs to detect seller listings where the same product ASIN has been posted more than once by different merchants. Write a SQL query to return the `asin` and the total number of distinct `seller_id` accounts listing that ASIN, for all products listed by 2 or more sellers. Sort the output by the listing count descending, then by `asin` ascending. Tables: - `product_listings` (`listing_id` INT, `asin` VARCHAR, `seller_id` INT, `price` DECIMAL)
High-Mileage Ride Cancellations
Uber Operations team is analyzing long-distance driver cancellations to reduce passenger churn. Write a SQL query to find all rides with `status = 'cancelled_by_driver'` that had an estimated distance of 15.0 miles or greater. Return the `trip_id`, `city_id`, `distance_miles`, and `fare_estimate`. Order by `distance_miles` descending. Tables: - `trips` (`trip_id` INT, `city_id` INT, `status` VARCHAR, `distance_miles` DECIMAL, `fare_estimate` DECIMAL)
Top-Rated Comedy Releases
Netflix Content Strategy team wants a list of all Comedy titles released between 2024 and 2026 with an IMDb rating of 8.0 or higher. Write a SQL query to return `title`, `release_year`, and `imdb_rating`. Order results by `imdb_rating` descending, then by `title` alphabetically. Tables: - `titles` (`title_id` INT, `title` VARCHAR, `genre` VARCHAR, `release_year` INT, `imdb_rating` DECIMAL)