Introduction to SQL

Selecting Columns

SQL is a language that is used to query and manage data.

Course Sections
Goals In this section, you will learn how to:
  • Select a single column from a table.
  • Select a column as an alias.

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_name
FROM table_name; 

Example

Here we have a table called users with the following columns: id , user_name , age , and city .

users
iduser_nameagecity
1Alice25New York
2Bob30London
3Carol35Paris

To retrieve the user_name column from the users table, you can use the following SQL statement:

 SELECT user_name
FROM 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.
iduser_nameagecity
1Alice25New York
2Bob30London
3Carol35Paris
Interactive
Select user_name

Exercise 1.2

Write a SQL statement to select the city column from the users table.
iduser_nameagecity
1Alice25New York
2Bob30London
3Carol35Paris
Interactive
Select city
  •  SELECT city 
    FROM users; 

Exercise 1.3

Write a SQL statement to select the age column from the users table.
iduser_nameagecity
1Alice25New York
2Bob30London
3Carol35Paris
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_name
FROM 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 name
FROM users; 

This will return the following result:

name
Alice
Bob
Carol

Exercise 1.4

Run the following SQL statement to select the user_name column from the users table and give it an alias called name .
iduser_nameagecity
1Alice25New York
2Bob30London
3Carol35Paris
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 .
iduser_nameagecity
1Alice25New York
2Bob30London
3Carol35Paris
Interactive
select city as location
  •  SELECT city AS location
    FROM users; 

Selecting Multiple Columns

To select multiple columns we separate the column names with commas.

Syntax

 SELECT column1, column2
FROM table_name; 

Example

If we need to select age and location from the users table, we would write:

 SELECT age, location
FROM users 

Exercise 1.6

Write a SQL statement to select the city and age columns from the users .
iduser_nameagecity
1Alice25New York
2Bob30London
3Carol35Paris
Interactive
select city and age
  •  SELECT city, age
    FROM 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
iduser_nameagecity
1Alice25New York
2Bob30London
3Carol35Paris
Interactive
select all columns

Exercise 1.8

Write an SQL statement to select ALL columns.
iduser_nameagecity
1Alice25New York
2Bob30London
3Carol35Paris
Interactive
select all columns
  •  SELECT *
    FROM users; 

Quiz

Loading...

Loading...

summary We've learned how to
  • Select a single column
  • Select a column as an alias
  • Select multiple columns
  • Select all columns