SQL FOUNDATIONS:SELECT & Basic Queries

Learn SELECT basics, column order, aliases, and DISTINCT.

What is SELECT

The SELECT statement is used to retrieve data from a table. It is the most fundamental SQL command and is used in almost every query.

Basic Syntax

sql
SELECT column1, column2
FROM table_name;
  • SELECT specifies the columns to retrieve
  • FROM specifies the table

Example Dataset

Table: employees

idnamedepartmentsalary
1JohnHR40000
2AliceIT60000
3BobIT55000
4EmmaFinance70000

Selecting All Columns

sql
SELECT * FROM employees;

This retrieves all columns and all rows from the table.

Selecting Specific Columns

sql
SELECT name, salary FROM employees;

This retrieves only the name and salary columns.

Column Order Matters

sql
SELECT salary, name FROM employees;

The output will display columns in the same order as written in the query.

Using Aliases (Renaming Columns)

sql
SELECT name AS employee_name, salary AS income
FROM employees;
  • AS is used to rename columns in the output
  • Useful for readability and reporting

Removing Duplicate Values with DISTINCT

sql
SELECT DISTINCT department FROM employees;

This returns only unique department values.

Key Concepts

  • SELECT * is useful for exploration but not recommended in production
  • Always select only required columns for efficiency
  • Use DISTINCT when you need unique values
  • Use aliases to make results clearer

SELECT & Basic Queries Missions

Solve exercises in sequence to unlock the next mission.

1

Retrieve all columns

Retrieve all columns from the employees table.

Solve Mission
2

Select name and department

Retrieve only the name and department columns.

Locked
3

Salary then name

Retrieve salary and name, in that order.

Locked
4

Rename name column

Rename the column name to employee_name.

Locked
5

Distinct departments

Retrieve unique department values.

Locked
6

Salary only

Retrieve only the salary column.

Locked
7

Rename salary as income

Retrieve name and salary, and rename salary as income.

Locked