Hi everyone 🙂 Hope you are doing great. Today we are going to learn about SQL Commands. The SQL commands are categorized into four categories;

  • DQL
  • DDL
  • DML
  • DCL

Firstly we are getting to study DQL category commands; There are more easy-to-learn commands 😉 Let’s start!

DQL (Data Query Language)

DQL is employed to fetch/view Information from tables, either supported some conditions or not. It is up to us what is required, we can write queries accordingly.
The SELECT SQL command is employed to achieve this.


There are various scenarios, during which we’d like to fetch information, we’ll discuss them one by one. I will attempt to cover all, just in case I forget to say anything you’ll tell me within the comment section also, I will attempt to cover that also 🙂

1- All Rows and All columns :

There are two ways to achieve this;
i) First is to say all the names of columns within the table that you simply want to fetch. It is good if you’ve got few columns within the table but if there are several columns in hundreds, then it’ll be a hectic task to do.

Syntax:
   SELECT ColumnName FROM TableName;
(Figure 1: Select all column data by using all column name)

ii) Other way is to use asterisk symbol (*).

Syntax
    SELECT * FROM TableName;
(Figure 2: Fetch all rows and all columns)

Note: There are some capabilities of SELECT statement like as;

Using Arithmetic operations:

We can apply arithmetic operations on the column. It doesn’t affect the particular table but data view only.

For example;
It may be a query during which I’m adding 300 into the salary. but it’ll not change the particular data into the salary column. It just show, how data will look if we add 300 into the salary.

(Figure 3: Arithmetic operation in SELECT Query)

2- Filtering Table Data :

While Fetching information or data from table, it’s very rare that we require all the info from table whenever . Hence SQL provides how to filter the info that’s required.

There are many ways for filtering table data, some of them are;
i- Selected columns and all rows
ii- Selected rows and all columns
iii- Selected column and selected rows

i- Selected Columns and All Rows:

Syntax
    SELECT columnName1, ColumnName2 FROM TableName;
(Figure 4 : Selected Column and All Rows)

ii-Selected rows and all Columns :

If we’d like any particular information, then Retrieval/fetching information must be supported to some conditions.
For condition, SQL provides WHERE clause, using this we will apply condition within the SELECT Query.

Syntax
    SELECT * From  TableName WHERE Conditions;
(Figure 5: Selected Rows and All columns)

As you’ll see here, it’s showing all the columns but the sole rows which are matching the condition.

Note : When specifying a condition in the where clause, all standard operator such as logical, arithmetic etc can be used.

iii- Selected Columns and Selected Rows :

It is to fetch the precise set of rows and columns from the table.

Syntax
    SELECT ColumnName1, ColumnName2 FROM TableName WHERE Conditions;
(Figure 6 : Selected columns and Selected rows)

Note: As you can see here, it is showing mentioned columns and rows which are matching the condition.

3- Eliminate Duplicate Rows :

A table can hold duplicate data. In this Scenario to fetch only unique rows, SQL provides the DISTINCT Clause.
It scan through all the rows present in the table and eliminate them that have exact content in each column.

Syntax
    SELECT DISTINCT columnName1, ColumnName2 FROM TableName;
(Figure 7: With Distinct Clause)
(Figure 8: Without Distinct clause)

Note: As you can see above, if we are not using Distinct clause it is showing duplicate rows.

Hurray, we have learned something new. Hope you people enjoyed while learning. Please try to practice these queries on your own. If you face any problems, you can let me know in the comment section. I will surely reply.
Happy Learning!

2 Replies to “How To Fetch Rows And Columns From The Table?”

Leave a Reply

Your email address will not be published. Required fields are marked *