SELECT Statement – Learn by ExamplesThe SELECT statement is the most used command in SQL. It is used to retrieve data from one or more tables in your database.
SELECT column1, column2, ... FROM table_name;
employeesImagine 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 |
Get all data from the employees table:
SELECT * FROM employees;
Get only first and last names:
SELECT first_name, last_name FROM employees;
WHEREGet employees from the IT department only:
SELECT * FROM employees WHERE department = 'IT';
ORDER BYGet all employees sorted by salary (highest first):
SELECT * FROM employees ORDER BY salary DESC;
LIMITGet only the top 2 highest paid employees:
SELECT * FROM employees ORDER BY salary DESC LIMIT 2;
Try to write a query that fetches first_name and department of employees hired after 2020-01-01.
SELECT first_name, department
FROM employees
WHERE hire_date > '2020-01-01';
SELECT * returns all columns.SELECT.WHERE filters rows based on conditions.ORDER BY sorts results ascending (ASC) or descending (DESC).LIMIT controls the number of rows returned.SELECT first_name FROM employees WHERE department='Sales' ORDER BY hire_date DESC LIMIT 3;
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!