B4Coder
0%
LessonAbout 5 min

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.

SQL
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:

SQL
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

  1. 1Show each subject's name and the teacher who teaches it, in that order: name, then teacher.

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.

SQL
SELECT * FROM subjects;

2Expected output

Your query should show

nameteacher
MathsMs. Rani
ScienceMr. Kumar
EnglishMs. Fernandes
TamilMr. Selvam
Social ScienceMs. 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 …

query.sql