Order by Clause and Distinct Keyword in SQL || Steem Alliance Community.

in hive-150122 •  7 months ago 

Hello Everyone,


White Soft Brown Professional Elegant Marketing Strategy Presentation 169_20240212_115249_0000.jpg

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.

Nosectorcusti_dname_1Town
110004270603SIKANDARPESHAWAR
210004270939TARIQALI PUR
310005715170ALI HAMZAGALA YOUSAF
410007796882IMRANKHANEWAL
51000953045UMARYAZMAN

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

NOsector
11000

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

NOsectorcust_idname_1
110004270603SIKANDAR
210004270939TARIQ
310005715170ALI HAMZA
410007796882IMRAN
51000953045UMAR

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.

7-1-SQL-DISTINCT.png
Img Src

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

NOsectorcust_idname_1
110005715170ALI HAMZA
210007796882IMRAN
310004270603SIKANDAR
410004270939TARIQ
51000953045UMAR

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

20230310_190323_0000.png

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

🆃🅷🅰️🅽🅺 🆈🅾️🆄

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Thank you, friend!
I'm @steem.history, who is steem witness.
Thank you for witnessvoting for me.
image.png
please click it!
image.png
(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.