Advertisement

Google Ad Slot: content-top

MySQL SELECT


The SELECT statement in MySQL is used to retrieve data from one or more tables in a database. It is the most commonly used SQL command for querying data.


Basic Syntax:


SELECT column1, column2 FROM table_name;



Here, column1, column2, ... are the field names of the table you want to select data from. If you want to select all the fields available in the table, use the following syntax:



SELECT * FROM students;


Table:


Below is a selection from the "students" table in the school_db database:


student_id

name 

gender

city

age

score

course_id

email

phone_number

1

Alice

Female

Delhi

20

85

101

NULL

1234567890

2

Bob

Male

Mumbai

22

75

NULL

bob@email.com

NULL

3

Charlie

Male

Delhi

21

95

102

NULL

NULL

4

David

Male

Bangalore

23

65

101

david@email.com

NULL

5

Eve

Female

Mumbai

20

80

103

NULL

NULL

6

Frank

Male

Delhi

22

90

103

NULL

NULL

7

Alice

Female

Mumbai

19

60

102

NULL

NULL

SELECT * Example

The following SQL statement selects ALL the columns from the "students" table:

Example
SELECT * FROM students;
Try it yourself

SELECT Column Example

The following SQL statement selects the "name" column from the "students" table:

Example
SELECT name FROM students;
Try it yourself

The MySQL SELECT DISTINCT Statement

The SELECT DISTINCT statement removes duplicate values and returns only unique records.


Syntax:


SELECT DISTINCT column_name FROM table_name;



Example (removing duplicates)


The following SQL statement selects only DISTINCT values from the "Country" column in the "students" table:

Example
SELECT DISTINCT name FROM students;
Try it yourself

SELECT Example Without DISTINCT


The following SQL statement selects all (including the duplicates) values from the "name" column in the "students" table:

Example
SELECT name FROM students;
Try it yourself

Key Differences: SELECT vs SELECT DISTINCT

Feature

SELECT

SELECT DISTINCT

Returns duplicates?

Yes

No

Use case

When you need all records

When you need unique values

Performance

Faster

Slightly slower (removes duplicates)