MYSQL Tutorial



MySQL SELECT


MySQL SELECT Statement – Learn by Examples

The SELECT statement is the most used command in SQL. It is used to retrieve data from one or more tables in your database.

🔹 Basic Syntax

SELECT column1, column2, ...
FROM table_name;
  

🔹 Example Table: employees

Imagine we have this employees table:

| employee_id | first_name | last_name | department | salary | hire_date  |
|-------------|------------|-----------|------------|--------|------------|
| 1           | John       | Doe       | Sales      | 50000  | 2020-01-10 |
| 2           | Jane       | Smith     | Marketing  | 60000  | 2019-05-15 |
| 3           | Mike       | Johnson   | IT         | 70000  | 2021-03-01 |
  

🔹 Example 1: Select All Columns

Get all data from the employees table:

SELECT * FROM employees;
  

🔹 Example 2: Select Specific Columns

Get only first and last names:

SELECT first_name, last_name FROM employees;
  

🔹 Example 3: Filter Rows with WHERE

Get employees from the IT department only:

SELECT * FROM employees WHERE department = 'IT';
  

🔹 Example 4: Sort Results with ORDER BY

Get all employees sorted by salary (highest first):

SELECT * FROM employees ORDER BY salary DESC;
  

🔹 Example 5: Limit Number of Rows with LIMIT

Get only the top 2 highest paid employees:

SELECT * FROM employees ORDER BY salary DESC LIMIT 2;
  

🔹 Interactive Challenge

Try to write a query that fetches first_name and department of employees hired after 2020-01-01.

Click here for the answer
SELECT first_name, department
FROM employees
WHERE hire_date > '2020-01-01';
    

🔹 Summary

  • SELECT * returns all columns.
  • You can select specific columns by listing them after SELECT.
  • WHERE filters rows based on conditions.
  • ORDER BY sorts results ascending (ASC) or descending (DESC).
  • LIMIT controls the number of rows returned.
Pro tip: You can combine these clauses for powerful queries. For example, SELECT first_name FROM employees WHERE department='Sales' ORDER BY hire_date DESC LIMIT 3;

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review