---Advertisement---

MySQL SELECT Statement: A Comprehensive Guide with Examples Best 2025

By Manisha

Updated On:

---Advertisement---
MySQL SELECT Statement

MySQL SELECT Statement: The MySQL SELECT statement is one of the most fundamental commands in SQL, used to retrieve data from a database. It allows you to fetch specific columns, filter records, sort results, and even perform calculations on data. This guide covers the SELECT statement in detail, with syntax, examples, and best practices for writing efficient queries.

  1. What is a MySQL SELECT Statement?
  2. SQL SELECT Syntax
  3. Retrieving All Data from a Table
  4. Selecting Specific Columns
  5. Using WHERE Clause to Filter Results
  6. Sorting Results with ORDER BY
  7. Grouping Data with GROUP BY
  8. Using Aggregate Functions
  9. Using Aliases for Better Readability
  10. MySQL SELECT with JOINs
  11. Why Use MySQL Workbench?
  12. Summary

MySQL SELECT Statement: The SELECT statement is used to retrieve records from a database table based on given criteria. It can be executed using various database management tools, programming languages (such as PHP and Python), or directly through MySQL Workbench.


The general syntax for the SELECT statement in MySQL is:

sql

CopyEdit

SELECT [DISTINCT | ALL] { * | column_name [AS alias_name] } 

FROM table_name 

[WHERE condition] 

[GROUP BY column_name(s)] 

[HAVING condition] 

[ORDER BY column_name(s) ASC|DESC];

  • SELECT โ†’ Keyword to retrieve data
  • DISTINCT | ALL โ†’ Optional; DISTINCT removes duplicates, ALL (default) returns all records
  • * โ†’ Selects all columns
  • column_name AS alias_name โ†’ Renames a column for readability
  • FROM table_name โ†’ Specifies the table to retrieve data from
  • WHERE condition โ†’ Filters records based on conditions
  • GROUP BY column_name(s) โ†’ Groups records with the same values
  • HAVING condition โ†’ Filters grouped results
  • ORDER BY column_name ASC|DESC โ†’ Sorts results in ascending or descending order

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

sql

CopyEdit

SELECT * FROM members;

Example Output:

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

MySQL SELECT Statement: To fetch only certain columns, specify them in the query:

sql

CopyEdit

SELECT full_names, gender, email FROM members;


To retrieve data based on a condition, use WHERE:

sql

CopyEdit

SELECT * FROM members WHERE gender = ‘Female’;


To display results in a sorted order, use ORDER BY:

sql

CopyEdit

SELECT full_names, date_of_birth FROM members ORDER BY date_of_birth DESC;


The GROUP BY clause groups records with the same values:

sql

CopyEdit

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


SQL provides built-in functions like COUNT(), SUM(), AVG(), MAX(), and MIN().

Example: Count total male members:

sql

CopyEdit

SELECT COUNT(*) AS male_members FROM members WHERE gender = ‘Male’;


To rename columns in the result set, use AS:

sql

CopyEdit

SELECT full_names AS Name, date_of_birth AS Birthdate FROM members;


To retrieve data from multiple tables, use JOIN. Example:

sql

CopyEdit

SELECT members.full_names, movies.title 

FROM members 

JOIN rentals ON members.membership_number = rentals.member_id 

JOIN movies ON rentals.movie_id = movies.movie_id;


  • Provides a visual query builder
  • Auto-generates SQL queries
  • Improves productivity with query execution tools

  • The SELECT statement retrieves data from MySQL databases
  • Use WHERE for filtering and ORDER BY for sorting
  • Use GROUP BY and HAVING for advanced data aggregation
  • JOIN is used to fetch data from multiple tables
  • MySQL Workbench simplifies query generation

MY SQL WHERE CLAUSE

Download My SQL

Leave a Comment

Index