How to Use SELECT In Order BY Specific Ids in SQL?
The order by the statement is used in SQL to sort the result set in ascending or descending by mentioning it in the suffix as DESC (for descending) and for ASC(for ascending). In this article, we will be doing order by on a database with some specified values of the column only.
So let’s start by creating a Database first.
Step 1: Create a database
Query:
CREATE DATABASE GFG
Step 2: Use database
Query:
USE GFG
Step 3: Create a table
Query:
CREATE TABLE s_marks ( studentid int PRIMARY KEY, subjectid VARCHAR(10), professorid int )
Step 4: Insert some data in the table
Query:
INSERT INTO [dbo].[s_marks] ([studentid] ,[subjectid] ,[professorid]) VALUES(1, 'DSA', 6) GO INSERT INTO [dbo].[s_marks] ([studentid] ,[subjectid] ,[professorid]) VALUES(2, 'Compiler', 7) GO INSERT INTO [dbo].[s_marks] ([studentid] ,[subjectid] ,[professorid]) VALUES(3, 'ML', 8) GO INSERT INTO [dbo].[s_marks] ([studentid] ,[subjectid] ,[professorid]) VALUES(4, 'AI', 9) GO
Step 5: Get the table data according to student id and order by using some ids.
Query:
SELECT studentid, subjectid FROM s_marks WHERE studentid IN (1,4) ORDER BY studentid DESC
Output:
So we can see that data is printed successfully with the order by as well as the respective ids.
Please Login to comment...