DEV Community

Akshat Sharma
Akshat Sharma

Posted on

Day 4 of SQL Series || SELECT Statement Use cases

Hey there 👋

Hope you are doing well 🙂

In the last blog we have discussed about DML (Data Manipulation Language) commands. In this blog we are going to see some of the basic commands of SQL. We will be discussing about SELECT command and different use cases in this blog.

So let's get started 🔥
Image description

SELECT Statement

The SELECT statement is used to select data from the database or more specifically table.
To understand this in a more better way we are going to use our Student table from last blog. Here is the table -:
Image description

Use Case 1

Selecting all rows of table -:
Image description
So here Select is used to select data '*' refers to all the rows or complete data.
Here is output -:
Image description
I can't able to show complete output here but you will get it when you try it on your own.

Use Case 2

Selecting specific column from table -:
Image description
This query will display complete data of Name column.
Image description

Use Case 3

Select distinct items from table -:
For this I'll add some duplicate rows in table. So our table will look like -:
Image description
Now we will select only distinct rows here.
Image description
So here we are using distinct to select distinct rows and '*' for selecting all columns. We can select distinct values from a particular column too for this you just need to replace * from column name.

Image description

Use Case 4

Selecting all the students of Class 5B -:
Image description
In the above query where is used to filter records based on specified condition. Here we are selecting all the students of Class 5B.
Here is the output -:
Image description
We can also select distinct entries all we need to use is Distinct keyword here.

Use Case 5

Selecting all the students whose Roll_no is between 12 and 21-:
Image description
Here we have applied a condition on Roll_no and based on that condition output is generated.
Image description
You can apply different operations here for example selecting students with roll_no greater than a particular value or selecting students having roll_no equal to particular value.

Use Case 6

Selecting all the students whose name starts with J-:
Image description
Here we have used like and 'J%'.
The like operator is used with where clause to search for a pattern in string.
% represents zero, one, or multiple characters, so here we have given the starting character of the string i.e. 'J'.
Image description

Use Case 7

Selecting only first 3 records from table -:
Image description
Here we have used limit keyword to show top 3 rows this is a MySQL based syntax different management systems have different syntax like in Oracle based system we can do it by just applying a condition on ROWNUM or you can use FETCH FIRST.
Image description

So these were some basic Select command usecases. This was just an introduction we will see some good use cases in upcoming blogs.
In the next blog we will see operators and wildcards and its use cases.

I hope you liked my blog. Please leave some ❤ and don't forget to follow me.
Also have you checked my Instagram page where I upload necessary resources for software engineers.
If not check it out here -:
👉 Instagram: https://www.instagram.com/fluxx_96/
👉 YouTube: https://www.youtube.com/@Fluxx-n4q/shorts

Thankyou 💙

Top comments (0)