Order by and Distinct in SQL
Before ORDER BY
Clause
FRUIT_NAME
|
UNIT_PRICE
|
QUANTITY
|
PURCHASE_DATE
|
Apple
|
100
|
60
|
01-Jan-20
|
Orange
|
110
|
40
|
01-Jan-20
|
Apple
|
105
|
45
|
02-Jan-20
|
kiwi
|
190
|
22
|
02-Jan-20
|
Banana
|
70
|
20
|
03-Jan-20
|
Pumpkin
|
14
|
120
|
03-Jan-20
|
Banana
|
60
|
23
|
04-Jan-20
|
Tomato
|
13
|
15
|
04-Jan-20
|
Pumpkin
|
11
|
12
|
05-Jan-20
|
Tomato
|
22
|
100
|
05-Jan-20
|
Apple
|
101
|
11
|
06-Jan-20
|
kiwi
|
200
|
19
|
06-Jan-20
|
Apple
|
115
|
12
|
07-Jan-20
|
Tomato
|
11
|
21
|
07-Jan-20
|
Banana
|
65
|
25
|
08-Jan-20
|
Pumpkin
|
16
|
33
|
08-Jan-20
|
Apple
|
105
|
12
|
09-Jan-20
|
Tomato
|
12
|
29
|
09-Jan-20
|
AFTER ORDER BY
Clause
FRUIT_NAME
|
UNIT_PRICE
|
QUANTITY
|
PURCHASE_DATE
|
Apple
|
100
|
60
|
01-Jan-20
|
Apple
|
105
|
45
|
02-Jan-20
|
Apple
|
101
|
11
|
06-Jan-20
|
Apple
|
115
|
12
|
07-Jan-20
|
Apple
|
105
|
12
|
09-Jan-20
|
Banana
|
70
|
20
|
03-Jan-20
|
Banana
|
60
|
23
|
04-Jan-20
|
Banana
|
65
|
25
|
08-Jan-20
|
kiwi
|
190
|
22
|
02-Jan-20
|
kiwi
|
200
|
19
|
06-Jan-20
|
Orange
|
110
|
40
|
01-Jan-20
|
Pumpkin
|
14
|
120
|
03-Jan-20
|
Pumpkin
|
11
|
12
|
05-Jan-20
|
Pumpkin
|
16
|
33
|
08-Jan-20
|
Tomato
|
13
|
15
|
04-Jan-20
|
Tomato
|
22
|
100
|
05-Jan-20
|
Tomato
|
11
|
21
|
07-Jan-20
|
Tomato
|
12
|
29
|
09-Jan-20
|
--====================================================
Order by in SQL
--====================================================
SELECT * FROM
XXSD_FOOD_TL
ORDER BY
FRUIT_NAME DESC
SELECT * FROM
XXSD_FOOD_TL
ORDER BY
FRUIT_NAME,
QUANTITY DESC
--====================================================
Different way to use Order by in SQL
--====================================================
SELECT * FROM
XXSD_FOOD_TL
ORDER BY 4 DESC
SELECT * FROM
XXSD_FOOD_TL
ORDER BY 1,2,3
--====================================================
Distinct
keyword in SQL
--====================================================
select DISTINCT
PURCHASE_DATE from
XXSD_FOOD_TL ORDER BY 1
SELECT DISTINCT
FRUIT_NAME FROM
XXSD_FOOD_TL
Comments
Post a Comment