0% found this document useful (0 votes)
181 views2 pages

SQL Assign 2 Ans

The document describes creating two tables, tblStudent and tblScholarship, with student details like name, admission date, branch etc. It then lists 15 queries to retrieve data from the tables. The queries include combining first and last name, filtering by name, sorting by name and fee, extracting date parts, calculating date differences, and formatting date fields.

Uploaded by

Vishwa Vinith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
181 views2 pages

SQL Assign 2 Ans

The document describes creating two tables, tblStudent and tblScholarship, with student details like name, admission date, branch etc. It then lists 15 queries to retrieve data from the tables. The queries include combining first and last name, filtering by name, sorting by name and fee, extracting date parts, calculating date differences, and formatting date fields.

Uploaded by

Vishwa Vinith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

sql Assignment -2

Create table tblStudent


(
Studentid int (50)
FirstName varchar(50),
LastName varchar(50),
Admission_fee int,
Admission_date datetime,
Branch varchar(50),
)

Create table tblScholarship


(
Student_ref_id int,
Scholarship_Date date,
Scholarship_Amount int
)

1) Write a query to combine FirstName and LastName and display it as "Full Name".
Ans-Select FirstName +' '+ LastName As "Full Name" from tblstudent

2) List all students whose first name start with 'Ma' or 'Da'.
Ans-SELECT FirstName, LastName, Branch FROM tblStudent WHERE firstname
LIKE'%Ma%' OR firstname LIKE '%Da%'

3) Get all students details from the tblStudent table order by LastName
Ascending and Admission fees descending.
Ans-Select * from tblStudent order by LastName asc, Admission_Fee desc

4) Get position of 'v' in name 'David' from tblstudent.


Ans-Select CHARINDEX('v', FirstName,0) from tblstudent where FirstName = 'David'

5) Get FristName from tblStudent table after replacing 'a' with '$'.
Ans-Select REPLACE(FirstName,'a','$') from tblStudent

6) Get student details from tblStudent table whose admission year is "2015".
Ans-Select * from tblStudent where SUBSTRING (convert (varchar, Admission_date,
103),7,4) ='2015'
7) Get student details from tblStudent table whose admission date is after January 31st.
Ans-Select * from tblStudent where Admission_date >'01/31/2016'

8) Get student details from tblStudent table whose admission month is "January".
Ans-Select * from tblStudent where SUBSTRING (convert (varchar,Admission_date,
100), 1,3)='Jan'

9) Get only month part of admission date from tblStudent.


Ans-select DATEPART(MONTH, admission_date) from tblStudent

10) Get all student details from tblStudent table whose admission date between '2015-01-
01' and '2016-01-01'.
Ans-select * from tblStudent where admission_date between '2015-01-01' and '2016-01-01'

11) Get the first name, last name, current date, admission date and difference between
current date and admission date in days.
Ans-select FirstName, LastName, GETDATE() as 'Current_date', Admission_date,
DATEDIFF(DD, Admission_date, GETDATE()) As days from tblStudent

12) Show "AdmissionDate" in "dd mmm yyyy" format, ex- "06 May 2016".
Ans-select CONVERT(varchar(30), admission_date, 106) from tblStudent

13) Show "AdmissionDate" in "yyyy/mm/dd" format, ex- "2016/05/06".


Ans-select CONVERT(varchar(30), admission_date, 111) from tblStudent

14) Select no of students get admission with respect to year and month from tblStudent
table.
Ans-select datepart (YYYY,Admission_date) Admission_Year, datepart
(MM,Admission_date)Admission_Month,count(*) Total_Student from tblStudent group by
datepart(YYYY,Admission_date),datepart(MM,Admission_date)

15) Get first name, admission year, admission month and admission date from tblStudent
table.
Ans-select SUBSTRING (convert(varchar,admission_date,103),7,4) as Year,SUBSTRING
(convert(varchar,admission_date,100),1,3) as Month, SUBSTRING (convert (varchar,
admission_date,100),5,2) as Date from tblStudent

You might also like