Chapter 1 · Lesson 2 of 5
02. Choosing columns
Name the columns you want after SELECT, in the order you want them, instead of *.
Usually you don't want every column. Name the ones you want after SELECT, with commas between them, and only those come back, in the order you wrote them.
SELECT name, city FROM students;Result
name | city --------+----------- Asha | Chennai Ravi | Madurai Meera | Coimbatore Arjun | Chennai Divya | Salem Karthik | Madurai Priya | NULL Rahul | Chennai Sneha | Coimbatore Vikram | Trichy
The order is up to you. Put city first and it comes first:
SELECT city, name, class FROM students;Result
city | name | class -----------+---------+------ Chennai | Asha | 10 Madurai | Ravi | 10 Coimbatore | Meera | 9 Chennai | Arjun | 10 Salem | Divya | 9 Madurai | Karthik | 11 NULL | Priya | 11 Chennai | Rahul | 9 Coimbatore | Sneha | 10 Trichy | Vikram | 11
Your turn
1Steps
- 1Show each subject's name and the teacher who teaches it, in that order:
name, thenteacher.
Write it in query.sql. The tables are in the Tables tab, beside Results.
Use: FROM
Starting code
The editor starts empty. Copy this into it to begin, or write your own from scratch.
SELECT * FROM subjects;2Expected output
Your query should show
| name | teacher |
|---|---|
| Maths | Ms. Rani |
| Science | Mr. Kumar |
| English | Ms. Fernandes |
| Tamil | Mr. Selvam |
| Social Science | Ms. Banu |
5 rows, in any order. The column names count too.
3Hints
Stuck? Open one hint at a time: each gives a bit more help.
Hint 1
Run it first: * shows every column, but you only want two of them.
Hint 2
Replace * with the two columns' names, with a comma between them.
Hint 3
The columns are name and teacher: SELECT name, teacher FROM …