MySQL SELECT Statement
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
๐งช Example
Suppose you have a table named students:
| id | name | age |
|---|---|---|
| 1 | Rahul | 20 |
| 2 | Priya | 22 |
| 3 | Aman | 21 |
Select all data:
๐ฏ 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.
| Function | Example |
|---|---|
| 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
| Feature | Example |
|---|---|
| Select all data | SELECT * FROM table; |
| Select columns | SELECT name, age FROM students; |
| Filter data | SELECT * FROM students WHERE age > 20; |
| Sort results | ORDER BY column ASC/DESC |
| Remove duplicates | SELECT DISTINCT column; |
| Limit rows | LIMIT number |
