Your First SELECT Query

The SELECT statement is the most fundamental SQL command. It retrieves data from one or more tables.

Basic Syntax

SELECT column1, column2, ...
FROM table_name;

Selecting All Columns

Use * (asterisk) to select every column:

SELECT * FROM employees;

This returns all rows with all columns. Try it in the editor!

Selecting Specific Columns

Often you only need certain columns. List them after SELECT:

SELECT name, department, salary
FROM employees;

This is better practice than SELECT * because:

  • It's clearer what data you need
  • It's faster (less data to transfer)
  • It won't break if columns are added/removed later

Column Aliases

You can rename columns in the output using AS:

SELECT
  name AS employee_name,
  salary AS annual_salary
FROM employees;

Ordering Results

Use ORDER BY to sort results:

-- Sort by salary (lowest first - ascending is default)
SELECT name, salary FROM employees ORDER BY salary;

-- Sort by salary (highest first)
SELECT name, salary FROM employees ORDER BY salary DESC;

-- Sort by department, then by name within each department
SELECT name, department, salary
FROM employees
ORDER BY department, name;

Limiting Results

Use LIMIT to return only a certain number of rows:

-- Get the top 3 highest-paid employees
SELECT name, salary
FROM employees
ORDER BY salary DESC
LIMIT 3;

DISTINCT Values

Use DISTINCT to remove duplicate values:

-- Get unique departments
SELECT DISTINCT department FROM employees;

Try It Yourself

Practice these queries in the editor:

  1. Select only name and email from employees
  2. Get all employees ordered by hire_date (newest first)
  3. Find the top 5 highest-paid employees
  4. List all unique departments
Take Quiz Next Lesson
SQL Editor Ctrl+Enter to run
Results
Run a query to see results here
Tables: