SQL Fundamentals
SQL-SQL-SELECT-Guide
The SELECT statement is one of the most fundamental and powerful commands in MySQL. This guide will help you master SELECT queries from basic to advanced concepts.
You'll Learn
- Basic SELECT statement syntax
- How to filter and sort data
- Working with multiple tables
- Query optimization techniques
Free Beta Access
The Most Beautiful Database IDE You've Been Looking For
Get early access to our beta version and help us shape the future of SQL learning.
1. Basic SELECT Structure
Every SELECT statement follows this basic structure:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Key Point
While we write SELECT first, MySQL actually processes the FROM clause first during execution.
2. Working with Sample Data
Let's use this sample employees table:
employees table
id | first_name | last_name | salary | department ----+-----------+-----------+---------+------------ 1 | John | Doe | 60000 | Sales 2 | Jane | Smith | 65000 | Marketing 3 | Bob | Johnson | 55000 | Sales 4 | Alice | Williams | 70000 | Engineering
Basic Queries
-- Select specific columns
SELECT first_name, last_name, salary
FROM employees;
-- Using WHERE clause
SELECT *
FROM employees
WHERE salary > 60000;
-- Using ORDER BY
SELECT first_name, last_name, salary
FROM employees
ORDER BY salary DESC;
3. Advanced Queries
Aggregations
-- Average salary by department
SELECT
department,
COUNT(*) as employee_count,
AVG(salary) as avg_salary
FROM employees
GROUP BY department;
Result: department | employee_count | avg_salary -------------+---------------+----------- Sales | 2 | 57500 Marketing | 1 | 65000 Engineering | 1 | 70000