42
loading...
This website collects cookies to deliver better user experience
Select [Column Name | *]
Where [Column Name] Like [criteria]
AND | OR [Column Name] [operator] [criteria]
Order By [Column Name] (asc/desc), [Column Name] (asc/desc),...
ascending order
or we can sort our specified column in desc descending order
. We can also use as many columns as we prefer and sort them in whatever order we prefer.The Order By clause will first sort the order of the first column with the method we specify, then from that result it will order the next column, in the order that we specified for that particular column and so on and so forth...
Remember to make sure that you are connected to the correct database, in this case AdventureWorks.
When we use an Order By clause and choose a column name, we do not need to specify asc if we want the column to be in ascending order. SSMS will order it in ascending order by default. But if you want to be declarative in your database then you would still type asc.
Select Distinct [Column Names | *]
Where [Column Name] Like [criteria]
AND | OR [Column Name] [operator] [criteria]
Order By [Column Name] (asc/desc), [Column Name] (asc/desc),...
Customer ID | Customer Name | Customer Address |
---|---|---|
C1 | Fred | ... |
C2 | Bert | ... |
C3 | Jane | ... |
Product ID | Product Name | Description |
---|---|---|
P1 | Shirt | ... |
P2 | Tie | ... |
P3 | Collar | ... |
Order ID | Customer ID | Product ID | Quantity |
---|---|---|---|
1000 | C1 | P1 | 1 |
1001 | C2 | P1 | 3 |
1002 | C1 | P3 | 1 |
1003 | C1 | P3 | 2 |
1004 | C2 | P2 | 4 |
1005 | C1 | P2 | 2 |
1006 | C3 | P3 | 1 |