- Practice
- About 14 min
- 5 practice questions
SQL basics: SELECT, WHERE and common commands
Read simple SQL queries and predict their results, including SELECT, WHERE, ORDER BY and aggregate functions.
If something looks wrong, please tell us.
SQL (Structured Query Language) is the standard language for working with relational databases. Papers often show a small table and a query and ask for the result.
Basic statements
| Statement | Purpose |
|---|---|
| SELECT | Retrieve data |
| INSERT | Add a new record |
| UPDATE | Change existing data |
| DELETE | Remove records |
| CREATE TABLE | Make a new table |
| WHERE | Filter rows by a condition |
| ORDER BY | Sort the results |
Example form: SELECT Name FROM Student WHERE Marks > 60; The query returns the names of students with marks above 60. Aggregate functions include COUNT, SUM, AVG, MAX and MIN.
Worked examples
Example 1. Student table: Asha 72, Bilal 55, Chitra 90, Dev 60. What does SELECT Name FROM Student WHERE Marks > 60 return? Marks above 60 are Asha (72) and Chitra (90). Dev's 60 is not greater than 60. Result: Asha, Chitra.
Example 2. For the same table, what does SELECT AVG(Marks) return? Sum = 72 + 55 + 90 + 60 = 277. Average = 277 / 4 = 69.25.
SELECT chooses columns, FROM names the table, WHERE filters rows, ORDER BY sorts.
Key takeaways: read a query from SELECT to WHERE, and check boundaries carefully: greater than excludes equal values.
Check your understanding
-
1. Student table marks: Asha 72, Bilal 55, Chitra 90, Dev 60. What does SELECT Name FROM Student WHERE Marks > 60; return?
- Asha, Chitra and Dev
- Bilal and Dev
- Asha and Chitra
- Chitra only
Show answer
Answer: C. Greater than 60 keeps 72 and 90. Dev's 60 is not greater than 60.
-
2. Student table marks: Asha 72, Bilal 55, Chitra 90, Dev 60. What does SELECT COUNT(*) FROM Student WHERE Marks >= 60; return?
- 2
- 4
- 1
- 3
Show answer
Answer: D. Greater than or equal to 60 keeps 72, 90 and 60: three rows.
-
3. Student table marks: Asha 72, Bilal 55, Chitra 90, Dev 60. What does SELECT AVG(Marks) FROM Student; return?
- 69.25
- 69
- 70
- 277
Show answer
Answer: A. Sum = 72 + 55 + 90 + 60 = 277; 277 / 4 = 69.25. (277 is the SUM.)
-
4. Which clause sorts the results of a query?
- WHERE
- ORDER BY
- FROM
- INSERT
Show answer
Answer: B. ORDER BY sorts; WHERE filters rows and FROM names the table.
-
5. Which statement makes a new table?
- INSERT
- UPDATE
- CREATE TABLE
- SELECT
Show answer
Answer: C. CREATE TABLE makes a table; INSERT adds a record to an existing one.
Sign in to keep track of the lessons you finish.