Course Content
MYSQL Tutorial
About Lesson

Basic SELECT Statement

In the world of databases, the ability to retrieve and manipulate data is fundamental. The SELECT statement is at the core of this process, allowing users to query a database and retrieve specific information. In this post, we’ll delve into the basics of the SELECT statement in MySQL, exploring its syntax and some common use cases.

Understanding the Syntax

Before diving into practical examples, it’s crucial to grasp the basic syntax of the SELECT statement. The general structure looks like this:

  • SELECT: Specifies the columns that you want to retrieve.
  • FROM: Specifies the table from which to retrieve the data.
  • WHERE: Optional, but allows you to filter the results based on a specified condition.

Retrieving Data from a Single Table

Let’s start with a simple example. Suppose we have a table named employees with columns employee_id, first_name, last_name, and salary. To retrieve all the information from this table, the query would be:

SELECT * FROM employees;

This will return all columns for all records in the employees table.

Filtering Data with WHERE Clause

The real power of the SELECT statement comes when you need to filter data. For instance, if you want to retrieve only the employees with a salary greater than 50000, the query would be:

SELECT * FROM employees
WHERE salary > 50000;

Sorting Results with ORDER BY

To further enhance your queries, you can sort the results using the ORDER BY clause. For instance, to retrieve employees sorted by salary in descending order:

SELECT * FROM employees
ORDER BY salary DESC;

Limiting Results with LIMIT

In scenarios where you only need a specific number of records, you can use the LIMIT clause. For example, to retrieve the top 10 highest-paid employees:

SELECT * FROM employees
ORDER BY salary DESC
LIMIT 10;