Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

SELECT Statement in MySQL | Querying in MySQL

Ujjwal Abhishek
Ujjwal Abhishek

SELECT Statement in MySQL

The SELECT statement is used for querying data. It allows you to select data from one or more tables.

Syntax:

SELECT column1, column 2,....
FROM table_name;                                                                             
  • In the above syntax for select statements, there can be one or multiple column names after the ‘SELECT’ keyword from which you want to select data. All the column names need to be separated by a comma (,).
  • ‘table_name’ denotes the name of the table from which you want to select the data.
  • The semicolon (;)is optional if there is only one statement. It is needed to separate multiple statements to execute them individually.
  • It's also not mandatory to write the SQL keywords in uppercase. It works fine in both upper and lower case but it is written in uppercase by convention.
  • MySQL evaluates the FROM clause before the SELECT clause at the time of execution.

Example : 

student  
roll_nonamestream
1AshishCSE
2PrashantCSE
3UjjwalCSE
4AnkitIT
Selecting data from a single column
SELECT name FROM student;

Output :

name
Ashish
Prashant
Ujjwal
Ankit
Selecting data from multiple columns
SELECT name, stream FROM student;

Output :

namestream
AshishCSE
PrashantCSE
UjjwalCSE
AnkitIT
Selecting data from all the columns

SELECT * FROM student;

Output:

roll_nonamestream
1AshishCSE
2PrashantCSE
3UjjwalCSE
4AnkitIT

SELECT statement without FROM clause

The SELECT statement can also be used without the FROM clause.

For Example: SELECT 5 + 5;

Output:

5+5
10

MySQL has many built-in functions like string, date, and math functions. They can be used with the SELECT statement.

For Example: 

1. SELECT date();

Output:

date()
2021 - 11 - 16

The date() is a built-in function of MySQL. It can be used with the SELECT statement. It returns the current date of the MySQL server as an output.

2. SELECT CONCAT('Be' , '  ' , 'Awesome');

Output:

CONCAT('Be' , '  ' , 'Awesome')
Be Awesome

DUAL table

The DUAL table is not an actual table. It is a dummy table. It can be used with the SELECT statement without referencing any actual table.

SELECT DISTINCT statement in MySQL

The SELECT DISTINCT statement is used in MySQL to remove duplicate rows. It outputs every row a single time even if it appears multiple times in the actual table.

Syntax: 

SELECT DISTINCT column1, column2,... 
FROM table_name; 
SELECT DISTINCT with Single Column

For Example:

student 
namestream
VivekCSE
AnkitCSE
AnkitIT
VivekCSE

SELECT DISTINCT name FROM student;

Output:

name
Vivek
Ankit

Note: If there are multiple NULL in a column the SELECT DISTINCT clause will return only one NULL in the output. 

SELECT DISTINCT with multiple columns

In the case of multiple columns, it removes the duplicates by using the combination of the columns.

SELECT DISTINCT name, stream from student;

Output:

namestream
VivekCSE
AnkitCSE
AnkitIT
Ujjwal Abhishek
Ujjwal Abhishek
Ujjwal is final-year CSE Undergraduate, a competitive coder, and a web developer passionate about problem-solving and data structures and algorithms.
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet