Understand how to select a single column from a table.
Learn how to select a column as an alias.
Practice selecting multiple columns.
Selecting a Single Column
To retrieve data from a table, you can use the SELECT statement. The SELECT statement specifies the columns to be retrieved from the table.
Syntax
SELECT column_nameFROM table_name;
Example
Imagine we have a table called users with the following columns: id , user_name , age , and city .
users
id
user_name
age
city
1
Alice
25
New York
2
Bob
30
London
3
Carol
35
Paris
To retrieve the user_name column from the users table, you can use the following SQL statement:
SELECT user_nameFROM users;
This will return the following result:
user_name
Alice
Bob
Carol
Exercise 1.1
Run the following code to select the user_name column from the users table.
id
user_name
age
city
1
Alice
25
New York
2
Bob
30
London
3
Carol
35
Paris
Interactive
Select user_name
Exercise 1.2
Write a SQL statement to select the city column from the users table.
id
user_name
age
city
1
Alice
25
New York
2
Bob
30
London
3
Carol
35
Paris
Interactive
Select city
SELECT city FROM users;
Exercise 1.3
Write a SQL statement to select the age column from the users table.
id
user_name
age
city
1
Alice
25
New York
2
Bob
30
London
3
Carol
35
Paris
Interactive
select age
SELECT age FROM users;
Quiz
Loading...
Loading...
Selecting a Column as an Alias
What is an alias? An alias is a temporary name given to a column or a table in a SQL query. You can use aliases to make the output of a query more readable or to shorten the column or table names.
To select a column as an alias, you can use the AS keyword followed by the alias name. For example:
Syntax
SELECT column_name AS alias_nameFROM table_name;
Example
To select the user_name column from the users table and give it an alias name , you can use the following SQL statement:
SELECT user_name AS nameFROM users;
This will return the following result:
name
Alice
Bob
Carol
Exercise 1.4
Write a SQL statement to select the user_name column from the users table and give it an alias called name .
id
user_name
age
city
1
Alice
25
New York
2
Bob
30
London
3
Carol
35
Paris
Interactive
select user_name as name
SELECT user_name AS name FROM users;
Exercise 1.5
Write a SQL statement to select the city column from the users table and give it an alias location .
id
user_name
age
city
1
Alice
25
New York
2
Bob
30
London
3
Carol
35
Paris
Interactive
select city as location
SELECT city AS locationFROM users;
Selecting Multiple Columns
To select multiple columns we separate the column names with commas.
Sytnax
SELECT column1, column2FROM table_name;
Example
If we need to select age and location from the users table, we would write:
SELECT age, locationFROM users
Exercise 1.6
Write a SQL statement to select the city and age columns from the users .
id
user_name
age
city
1
Alice
25
New York
2
Bob
30
London
3
Carol
35
Paris
Interactive
select city and age
SELECT city, ageFROM users;
Selecting All Columns
It can be too much effort to type in all columns, and sometimes we may not know what all the columns are. In this case, we can use the * symbol to select all columns.
Syntax
SELECT *FROM table_name;
Example
SELECT *FROM users;
Exercise 1.7
Run the following code to select ALL columns
id
user_name
age
city
1
Alice
25
New York
2
Bob
30
London
3
Carol
35
Paris
Interactive
select all columns
Exercise 1.8
Write an SQL statement to select ALL columns.
id
user_name
age
city
1
Alice
25
New York
2
Bob
30
London
3
Carol
35
Paris
Interactive
select all columns
SELECT *FROM users;
Loading...
Loading...
Summary
To select a single column, use the SELECT statement followed by the column name.
To select a column as an alias, use the AS keyword followed by the alias name.
To select multiple columns, separate the column names with commas.