MySQL SELECT Statement

MySQL Tutorial

MySQL SELECT Statement

The SELECT statement in MySQL is used to retrieve data from one or more database tables. It is the most commonly used SQL command and forms the basis of most database queries.


 Basic Syntax


Or to select all columns:


 Example

Suppose you have a table named students:

idnameage
1Rahul20
2Priya22
3Aman21

Select all data:


Select specific columns:


 Using SELECT With WHERE Clause (Filtering Data)

The WHERE keyword filters records based on a condition.


 Using SELECT With ORDER BY (Sorting Results)


 Using SELECT With DISTINCT (Unique Values)

This removes duplicate values.


 Using SELECT With LIMIT (Limit Output Count)

This returns only the first 2 rows.


 Using SELECT With Aliases (Renaming Columns)

Aliases make results easier to read.


 Using SELECT With Functions

MySQL includes built-in functions for data analysis.

FunctionExample
COUNT()Total count of rows
MAX()Largest value
MIN()Smallest value
SUM()Total of values
AVG()Average value

Example:


 Combine Multiple Conditions

With AND and OR:


 Summary

FeatureExample
Select all dataSELECT * FROM table;
Select columnsSELECT name, age FROM students;
Filter dataSELECT * FROM students WHERE age > 20;
Sort resultsORDER BY column ASC/DESC
Remove duplicatesSELECT DISTINCT column;
Limit rowsLIMIT number

 The SELECT statement is essential for reading and analyzing database records.

You may also like...