📖 Scenario: You are managing a small bookstore database. You want to retrieve book information but only for books that are currently in stock. Controlling the query result is important to avoid showing irrelevant or incorrect data.
🎯 Goal: Build a simple PostgreSQL query that selects books only if their stock count is greater than zero, demonstrating why controlling query results matters.
📋 What You'll Learn
Create a table called books with columns id, title, and stock
Insert three books with specific stock values
Write a query to select only books with stock greater than zero
Add an ORDER BY clause to sort the results by title
💡 Why This Matters
🌍 Real World
Filtering and ordering data is essential in real-world databases to show only relevant information to users, such as available products in an online store.
💼 Career
Database developers and analysts must write queries that control results precisely to support business decisions and user interfaces.
Progress0 / 4 steps
1
Create the books table
Write a SQL statement to create a table called books with columns id as integer primary key, title as text, and stock as integer.
PostgreSQL
Hint
Use CREATE TABLE with the specified columns and types.
2
Insert sample book data
Insert three rows into the books table with these exact values: (1, 'The Great Gatsby', 3), (2, '1984', 0), and (3, 'To Kill a Mockingbird', 5).
PostgreSQL
Hint
Use a single INSERT INTO statement with multiple rows.
3
Select books with stock greater than zero
Write a SQL query to select id, title, and stock from books where stock is greater than zero.
PostgreSQL
Hint
Use WHERE stock > 0 to filter results.
4
Order the results by title
Modify the previous query to add an ORDER BY clause that sorts the results by title in ascending order.
PostgreSQL
Hint
Use ORDER BY title ASC to sort alphabetically.
Practice
(1/5)
1. Why is result control important when querying a database?
easy
A. It deletes unnecessary data from the database.
B. It makes the database run slower.
C. It helps you get clear and useful data by filtering and sorting.
D. It automatically fixes errors in your data.
Solution
Step 1: Understand what result control does
Result control means filtering, sorting, and limiting data to get clear results.
Step 2: Identify the benefit of result control
It helps avoid confusion and saves time by showing only useful data.
Final Answer:
It helps you get clear and useful data by filtering and sorting. -> Option C
Quick Check:
Result control = clear, useful data [OK]
Hint: Result control shapes data output for clarity and usefulness [OK]
Common Mistakes:
Thinking result control deletes data
Believing it slows down queries
Assuming it fixes data errors automatically
2. Which of the following is the correct SQL syntax to limit query results to 5 rows in PostgreSQL?
easy
A. SELECT * FROM table WHERE ROWS = 5;
B. SELECT * FROM table TOP 5;
C. SELECT * FROM table FETCH FIRST 5;
D. SELECT * FROM table LIMIT 5;
Solution
Step 1: Recall PostgreSQL syntax for limiting rows
PostgreSQL uses LIMIT to restrict the number of rows returned.
Step 2: Compare options with correct syntax
SELECT * FROM table LIMIT 5; uses LIMIT 5, which is correct. SELECT * FROM table TOP 5; uses TOP, which is SQL Server syntax. SELECT * FROM table FETCH FIRST 5; is invalid syntax (missing ROWS ONLY). SELECT * FROM table WHERE ROWS = 5; is invalid syntax.
Final Answer:
SELECT * FROM table LIMIT 5; -> Option D
Quick Check:
LIMIT 5 limits rows in PostgreSQL [OK]
Hint: Use LIMIT to restrict rows in PostgreSQL queries [OK]
Common Mistakes:
Using TOP instead of LIMIT
Trying FETCH FIRST without correct syntax
Using WHERE to limit rows
3. Given the table employees with columns id, name, and salary, what will this query return?
SELECT name FROM employees ORDER BY salary DESC LIMIT 3;
medium
A. The names of the 3 employees with the highest salaries.
B. The names of the 3 employees with the lowest salaries.
C. The names of all employees sorted by salary ascending.
D. An error because LIMIT cannot be used with ORDER BY.
Solution
Step 1: Understand ORDER BY salary DESC
This sorts employees by salary from highest to lowest.
Step 2: Apply LIMIT 3
Limits the output to the top 3 rows after sorting.
Final Answer:
The names of the 3 employees with the highest salaries. -> Option A
Quick Check:
ORDER BY DESC + LIMIT 3 = top 3 highest [OK]
Hint: ORDER BY DESC then LIMIT gets top N results [OK]
Common Mistakes:
Confusing DESC with ASC order
Thinking LIMIT applies before sorting
Believing LIMIT causes syntax error with ORDER BY
4. You wrote this query but it returns all rows instead of just 10:
SELECT * FROM sales WHERE amount > 100 LIMIT 10 OFFSET 5;
What is the most likely reason?
medium
A. The query is correct; it returns only 10 rows after skipping 5.
B. The WHERE condition is incorrect and matches all rows.
C. LIMIT and OFFSET must be in uppercase to work.
D. The OFFSET clause is not supported in PostgreSQL.
Solution
Step 1: Check syntax of LIMIT and OFFSET
PostgreSQL supports LIMIT and OFFSET in any case and syntax is correct.
Step 2: Understand WHERE clause effect
WHERE amount > 100 filters rows; if many match, LIMIT 10 OFFSET 5 returns 10 rows after skipping 5.
Final Answer:
The query is correct; it returns only 10 rows after skipping 5. -> Option A
Quick Check:
LIMIT + OFFSET works as expected in PostgreSQL [OK]
Hint: LIMIT and OFFSET control rows returned and skipped [OK]
Common Mistakes:
Thinking OFFSET is unsupported
Believing case sensitivity matters for keywords
Assuming WHERE is ignored
5. You want to get the top 3 highest paid employees from each department in a table employees(department, name, salary). Which approach best controls the result to achieve this?
hard
A. Use ORDER BY salary DESC LIMIT 3 without partitioning.
B. Use a window function with ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) and filter rows where row number ≤ 3.
C. Use GROUP BY department and MAX(salary) only.
D. Use LIMIT 3 and GROUP BY name.
Solution
Step 1: Understand the need for top N per group
We want top 3 salaries per department, not overall top 3.
Step 2: Identify correct SQL technique
Window functions with ROW_NUMBER() partitioned by department rank salaries per group.
Step 3: Filter rows with row number ≤ 3
This returns top 3 employees per department correctly.
Final Answer:
Use a window function with ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) and filter rows where row number ≤ 3. -> Option B
Quick Check:
Window functions + filtering = top N per group [OK]
Hint: Use ROW_NUMBER() with PARTITION BY for top N per group [OK]
Common Mistakes:
Using LIMIT without partitioning
Using GROUP BY without ranking
Confusing GROUP BY columns
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.