MySQL SELECT Statement
The SELECT statement in MySQL is used to retrieve data from one or more tables in a database.
Basic Syntax:
Example: Basic SELECT
SELECT column1, column2, ...
FROM table_name;
This SQL command selects specific columns (column1
, column2
, …) from a table (table_name
).
Selecting All Columns:
Example: SELECT *
SELECT * FROM users;
This SQL command selects all columns from the users
table.
Filtering Rows with WHERE:
Example: SELECT with WHERE
SELECT * FROM users
WHERE age > 25;
This SQL command selects all columns from the users
table where the age
is greater than 25.
Sorting Results with ORDER BY:
Example: SELECT with ORDER BY
SELECT * FROM users
ORDER BY last_name ASC;
This SQL command selects all columns from the users
table and sorts the results by last_name
in ascending order.
Limiting Rows with LIMIT:
Example: SELECT with LIMIT
SELECT * FROM users
LIMIT 10;
This SQL command selects the first 10 rows from the users
table.
Conclusion
The SELECT statement is fundamental in MySQL for querying and retrieving data based on specific conditions and requirements. Understanding its various clauses such as WHERE, ORDER BY, and LIMIT is essential for effective database management.