SQL Practice Logo

SQLPractice Online

SELECT & Data Retrieval: Examples

Module: SQL Fundamentals

Basic Column Selection

basic

HR needs a simple employee list with names and departments for a company directory

-- Select specific columns

SELECT

employee_id,

first_name,

last_name,

department,

email

FROM employees;

-- Result: Only these 5 columns returned, not all 20+ columns in table

employee_id | first_name | last_name | department | email

1 | John | Doe | Engineering | john.doe@company.com

2 | Jane | Smith | Sales | jane.smith@company.com

3 | Bob | Johnson | Marketing | bob.j@company.com

Explicitly listing columns is best practice. The database only reads and returns these specific columns, making the query faster and using less memory/network bandwidth.

All

With proper index on (employee_id, first_name, last_name, department, email), this can be an index-only scan - extremely fast even on millions of rows.

Column Aliases and Calculations

intermediate

Finance team needs employee salary report with annual salary and bonus calculations

SELECT

employee_id,

first_name,

last_name,

salary AS monthly_salary,

salary * 12 AS annual_salary,

salary * 12 * 0.15 AS annual_bonus,

CONCAT(first_name, ' ', last_name) AS full_name,

UPPER(department) AS dept_code

FROM employees

WHERE salary > 50000;

-- Aliases make output readable and allow referencing in application code

employee_id | first_name | last_name | monthly_salary | annual_salary | annual_bonus | full_name | dept_code

1 | John | Doe | 8000 | 96000 | 14400 | John Doe | ENGINEERING

2 | Jane | Smith | 7500 | 90000 | 13500 | Jane Smith | SALES

AS keyword creates column aliases. Calculations (salary * 12) are performed on each row. String functions (CONCAT, UPPER) transform data. Aliases are essential for readable output and application integration.

All

Calculations are cheap (microseconds per row). String functions like CONCAT are slightly more expensive but still fast. The WHERE clause filters before calculations, improving performance.