---Advertisement---

Complete Guide to MySQL SELECT Statement with Examples Great 2025

By Manisha

Updated On:

---Advertisement---
Complete Guide to MySQL SELECT Statement

What is the MySQL SELECT Statement?

MySQL SELECT Statement : The SELECT statement in MySQL is used to retrieve data from one or more tables in a database. It allows users to filter, sort, and manipulate query results based on different conditions.

This statement is commonly used in scripting languages such as PHP, Python, or directly in the MySQL command line.


MySQL SELECT Statement Syntax

sql

SELECT [DISTINCT|ALL] { * | fieldExpression [AS alias]}  

FROM table_name  

[WHERE condition]  

[GROUP BY field_name(s)]  

[HAVING condition]  

[ORDER BY field_name(s)];

  • SELECT – Used to specify which columns to retrieve.
  • DISTINCT | ALL – DISTINCT removes duplicate values; ALL (default) includes all values.
  • * – Selects all columns from the table.
  • fieldExpression [AS alias] – Retrieves specific columns or calculated fields with an alias.
  • FROM table_name – Specifies the table to fetch data from.
  • WHERE – Filters data based on conditions.
  • GROUP BY – Groups results by specified columns.
  • HAVING – Filters groups after aggregation.
  • ORDER BY – Sorts results based on one or more columns.

1. Selecting All Columns from a Table

MySQL SELECT Statement : To retrieve all columns from a table, use:

sql

SELECT * FROM members;

🔹 Example Output:

membership_numberfull_namesgenderdate_of_birthemail
1Janet JonesFemale1980-07-21janetjones@email.com
2Robert PhilMale1989-07-12robert@email.com

2. Selecting Specific Columns from a Table

MySQL SELECT Statement : If you need only certain columns, specify them in the query:

sql

SELECT full_names, gender, email FROM members;

🔹 Example Output:

full_namesgenderemail
Janet JonesFemalejanetjones@email.com
Robert PhilMalerobert@email.com

3. Using WHERE Clause to Filter Data

MySQL SELECT Statement : To retrieve specific records based on a condition, use WHERE:

sql

SELECT * FROM members WHERE gender = ‘Female’;

🔹 Example Output:

membership_numberfull_namesgenderdate_of_birth
1Janet JonesFemale1980-07-21

4. Using Expressions in SELECT Statements

MySQL SELECT Statement : You can use expressions to manipulate data in the query:

sql

SELECT full_names, YEAR(CURDATE()) – YEAR(date_of_birth) AS age FROM members;

🔹 Example Output:

full_namesage
Janet Jones44
Robert Phil35

5. Concatenating Strings in SELECT Query

You can combine multiple fields using the CONCAT() function:

sql

SELECT CONCAT(full_names, ‘ (‘, gender, ‘)’) AS member_info FROM members;

🔹 Example Output:

member_info
Janet Jones (Female)
Robert Phil (Male)

6. Using Aliases for Better Readability

To make your output more readable, use the AS keyword:

sql

SELECT full_names AS Name, gender AS Gender FROM members;

🔹 Example Output:

NameGender
Janet JonesFemale
Robert PhilMale

7. Sorting Results Using ORDER BY

To sort data in ascending (ASC) or descending (DESC) order:

sql

SELECT full_names, date_of_birth FROM members ORDER BY date_of_birth DESC;

🔹 Example Output:

full_namesdate_of_birth
Robert Phil1989-07-12
Janet Jones1980-07-21

8. Using GROUP BY for Aggregation

To count the number of members by gender:

sql

SELECT gender, COUNT(*) AS count FROM members GROUP BY gender;

🔹 Example Output:

gendercount
Female3
Male2

9. Using HAVING with GROUP BY

To filter aggregated results:

sql

SELECT gender, COUNT(*) AS count FROM members GROUP BY gender HAVING count > 1;

🔹 Example Output:

gendercount
Female3

10. Using MySQL Workbench for SELECT Queries

You can use MySQL Workbench to run SELECT queries without writing SQL manually.

  1. Open MySQL Workbench
  2. Right-click on the desired table → Select “Select Rows – Limit 1000”
  3. The generated query appears in the SQL editor.
  4. Click Execute to run the query and view the results.

Why Learn MySQL SELECT Instead of Using Workbench?

While MySQL Workbench provides a graphical way to query data, learning SQL gives you:
More control over queries
Ability to write complex queries not possible in GUI
Faster troubleshooting and debugging

You can use Workbench to generate SQL queries and customize them manually to improve efficiency.


Summary: Key Takeaways

✔ The SELECT statement is the most commonly used SQL command to fetch data.
✔ You can retrieve all columns (SELECT *) or specific columns (SELECT column_name).
✔ Use WHERE for filtering, ORDER BY for sorting, GROUP BY for aggregation.
Expressions and Aliases help in formatting results.
✔ MySQL Workbench can generate SELECT queries, but SQL knowledge helps in customizing them.


What’s Next?

Learn MySQL Joins to combine data from multiple tables!

Stay Updated: Bookmark this guide and follow for more SQL tutorials & database optimization tips!

How To Create Data Base

Download My SQL

Leave a Comment

Index