SELECT reads data from a table. Use * for every column or list the columns you need. DISTINCT removes duplicate rows from the result, AS gives a column a new name, and you can calculate new columns straight in the query, such as salary * 12.
These questions use a small employee table with 10 rows (set it up below).
SELECT column1, column2 FROM table_name;
SELECT DISTINCT column FROM table_name;
SELECT column, column * 12 AS new_name FROM table_name;
Set up the practice tables
Copy this script and run it once.
CREATE TABLE employee (
id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
salary INT,
department VARCHAR(50),
dob DATE
);
INSERT INTO employee VALUES
(101,'Jack',2000,'HR','1997-05-19'),
(102,'Jack',NULL,'HR',NULL),
(103,'Mack',6000,'Developer','1997-03-10'),
(104,'Peter',4000,'Tester','1998-07-16'),
(105,'Tom',3000,'HR','1998-11-03'),
(106,'Leo',2500,'Developer','1997-10-10'),
(107,'Roger',5300,'Accounts','1997-06-17'),
(108,'Mike',2000,NULL,'1998-03-09'),
(109,'Paul',4800,'Developer','1998-12-28'),
(110,'Hannah',2000,'Accounts','1999-09-26');Table: employee
| id | name | salary | department | dob |
|---|---|---|---|---|
| 101 | Jack | 2000 | HR | 1997-05-19 |
| 102 | Jack | NULL | HR | NULL |
| 103 | Mack | 6000 | Developer | 1997-03-10 |
| 104 | Peter | 4000 | Tester | 1998-07-16 |
| 105 | Tom | 3000 | HR | 1998-11-03 |
| 106 | Leo | 2500 | Developer | 1997-10-10 |
| 107 | Roger | 5300 | Accounts | 1997-06-17 |
| 108 | Mike | 2000 | NULL | 1998-03-09 |
| 109 | Paul | 4800 | Developer | 1998-12-28 |
| 110 | Hannah | 2000 | Accounts | 1999-09-26 |
How to use this page: read a question, write your own query, then open "Show the answer" and compare. Trying first is what makes it stick.
Question 1Display all the records of the employee table.
Show the answer and the output
SELECT * FROM employee;Output
| id | name | salary | department | dob |
|---|---|---|---|---|
| 101 | Jack | 2000 | HR | 1997-05-19 |
| 102 | Jack | NULL | HR | NULL |
| 103 | Mack | 6000 | Developer | 1997-03-10 |
| 104 | Peter | 4000 | Tester | 1998-07-16 |
| 105 | Tom | 3000 | HR | 1998-11-03 |
| 106 | Leo | 2500 | Developer | 1997-10-10 |
| 107 | Roger | 5300 | Accounts | 1997-06-17 |
| 108 | Mike | 2000 | NULL | 1998-03-09 |
| 109 | Paul | 4800 | Developer | 1998-12-28 |
| 110 | Hannah | 2000 | Accounts | 1999-09-26 |
Question 2Display the unique salaries of the employee table.
Show the answer and the output
SELECT DISTINCT salary FROM employee;Output
| salary |
|---|
| 2000 |
| NULL |
| 6000 |
| 4000 |
| 3000 |
| 2500 |
| 5300 |
| 4800 |
DISTINCT treats NULL as one value, so it appears once in the result.
Question 3Display the name and dob columns of the employee table.
Show the answer and the output
SELECT name, dob FROM employee;Output
| name | dob |
|---|---|
| Jack | 1997-05-19 |
| Jack | NULL |
| Mack | 1997-03-10 |
| Peter | 1998-07-16 |
| Tom | 1998-11-03 |
| Leo | 1997-10-10 |
| Roger | 1997-06-17 |
| Mike | 1998-03-09 |
| Paul | 1998-12-28 |
| Hannah | 1999-09-26 |
Question 4Display id, name and the annual salary with the column name 'annual_salary'.
Show the answer and the output
SELECT id, name, salary * 12 AS annual_salary FROM employee;Output
| id | name | annual_salary |
|---|---|---|
| 101 | Jack | 24000 |
| 102 | Jack | NULL |
| 103 | Mack | 72000 |
| 104 | Peter | 48000 |
| 105 | Tom | 36000 |
| 106 | Leo | 30000 |
| 107 | Roger | 63600 |
| 108 | Mike | 24000 |
| 109 | Paul | 57600 |
| 110 | Hannah | 24000 |
Anything multiplied by NULL is NULL, so employees with no salary show NULL.
Question 5Display the unique combinations of salary and department.
Show the answer and the output
SELECT DISTINCT salary, department FROM employee;Output
| salary | department |
|---|---|
| 2000 | HR |
| NULL | HR |
| 6000 | Developer |
| 4000 | Tester |
| 3000 | HR |
| 2500 | Developer |
| 5300 | Accounts |
| 2000 | NULL |
| 4800 | Developer |
| 2000 | Accounts |
With two columns, DISTINCT removes rows where the pair of values repeats.
Question 6Display the id and the name with the salary in parentheses, with the result column named emp_detail.
Show the answer and the output
SELECT id, CONCAT(name, '(', salary, ')') AS emp_detail FROM employee;Output
| id | emp_detail |
|---|---|
| 101 | Jack(2000) |
| 102 | NULL |
| 103 | Mack(6000) |
| 104 | Peter(4000) |
| 105 | Tom(3000) |
| 106 | Leo(2500) |
| 107 | Roger(5300) |
| 108 | Mike(2000) |
| 109 | Paul(4800) |
| 110 | Hannah(2000) |
CONCAT returns NULL if any value inside it is NULL. Jack (102) has no salary, so his emp_detail is NULL. Use CONCAT_WS or IFNULL if you want to avoid that.
Want these as PDFs? Download the question PDF or the answer PDF to practise offline.
