Order by Clause and Distinct Keyword in SQL || Steem Alliance Community.
Hello Everyone,
How are you, i hope you guys will happy and well. By thre grace of God, i am also well. Today i am here with new post of SQL and PL/SQL series in which i am going to discuss about Distinct key word and order by clause.
Distinct is a keyword that is used to get the unique values from the tables applying on specific / combination columns, it show the unique value to avoid the data duplication. The syntax of distinct keyword is very simple.
SELECT DISTINCT name_of_columns
FROM table_name;
For example I have a table with multiple rows and columns. I want to filter out the data duplication.
No | sector | custi_d | name_1 | Town |
---|---|---|---|---|
1 | 1000 | 4270603 | SIKANDAR | PESHAWAR |
2 | 1000 | 4270939 | TARIQ | ALI PUR |
3 | 1000 | 5715170 | ALI HAMZA | GALA YOUSAF |
4 | 1000 | 7796882 | IMRAN | KHANEWAL |
5 | 1000 | 953045 | UMAR | YAZMAN |
I get the above data from table by using the below mentioned query
select sector , cust_id , name_1 , Town from users where ROWNUM <=5
Now we use the distinct keyword on just sector column then see the result.
select DISTINCT sector from users where ROWNUM <=5
NO | sector |
---|---|
1 | 1000 |
Now we use the distinct keyword on more than one column then see the result.
select DISTINCT sector , cust_id , name_1 from users where ROWNUM <=5
NO | sector | cust_id | name_1 |
---|---|---|---|
1 | 1000 | 4270603 | SIKANDAR |
2 | 1000 | 4270939 | TARIQ |
3 | 1000 | 5715170 | ALI HAMZA |
4 | 1000 | 7796882 | IMRAN |
5 | 1000 | 953045 | UMAR |
This time you can see that sector is showing duplication due to using the distinct keyword on more than one column. Sector is repeating on the base of cust_id and name_1 because name_1 and cust_id of users are not same but sector of every user is same.
Basically, Order by clause is used for data sorting. In the daily routine working, we use it for data sorting in descending and ascending. For data sorting is ascending, we use the ASC keyword. For data sorting in descending. We use the DESC keyword.
See the result by using ASC keyword. I am using the order_by clasue on name_1 column.
select sector , cust_id , name_1 from users where ROWNUM <=5 order by name_1 asc ;
See the below result
NO | sector | cust_id | name_1 |
---|---|---|---|
1 | 1000 | 5715170 | ALI HAMZA |
2 | 1000 | 7796882 | IMRAN |
3 | 1000 | 4270603 | SIKANDAR |
4 | 1000 | 4270939 | TARIQ |
5 | 1000 | 953045 | UMAR |
Same as, it is for desc keyword. With desc keyword data will show opposite as compare the above result. Last row will show on first No and remaining rows will shuffle accordingly.
It would be my pleasure if you will share your opinion and reviews on this topic
Please cast witness vote to @bangla.Witness or set proxy to @rme.
_
Vote @bangla.witness as witness
Set Proxy @rme as proxy
Special Thanks.
Cc: @rme
Cc: @hungry-griffin
Cc: @steemcurator02
Thanks for giving your precious time to my post.
Follow | Upvote | Share | Comments
Follow me on Instagram.
Instagram
🆃🅷🅰️🅽🅺 🆈🅾️🆄
Thank you, friend!
I'm @steem.history, who is steem witness.
Thank you for witnessvoting for me.
please click it!
(Go to https://steemit.com/~witnesses and type fbslo at the bottom of the page)
The weight is reduced because of the lack of Voting Power. If you vote for me as a witness, you can get my little vote.
X Promotion Post.
https://twitter.com/alimhr96/status/1757080701764264346?t=L2oRJtw0zDPYxKQEyPdLUw&s=19