Queries
Queries
❖
Date and time functions in SQL)
13. Display the average price of each type of vehicle having quantity more
than 20.
Select Type, avg(price) from vehicle where qty>20 group by Type;
14 . Count the type of vehicles manufactured by each company.
Select Company, count (distinct Type) from Vehicle group by Company;
26 Use the select command to get the details of the students with marks
more than 80.
Ans.:
select * from student where marks>80;
27 Find the min, max, sum, and average of the marks in a student
marks table.
Ans.:
select max(marks), min(marks), sum(marks) , avg(marks) from student;
28 Find the total number of customers from each country in the table
(customer ID, customer Name, country) using group by.
Ans.:
select country, count (customer ID) from customer group by country;
Data
301 Aarav Mehta 8.9 2024-01-15 9:00:00
Science
Mathematic
302 Diya Kapoor 8.75 2024-01-15 8:45:00
s
Mathematic
308 Manav Gupta 7.85 2024-01-15 7:15:00
s
Program Table
35. Create a SQL query to retrieve "FIRST_NAME" from the Student table in
uppercase, and utilize the ALIAS name as STUDENT_NAME.
SQL query:
SELECT UPPER(FIRST_NAME) AS STUDENT_NAME
FROM Student;
Output:
STUDENT_NAME
Aarav
DIYA
SIDDHARTH
PRIYA
AAROHI
VIKRAM
STUDENT_NAME
RIYA
MANAV
36. Write a SQL query that prints the first three characters of FIRST_NAME
from the Student table.
SQL query
SELECT SUBSTRING (FIRST_NAME, 1, 3) AS First_Three_Chars
FROM Student;
Output:
First_Three_Chars
Aar
Diy
Sid
Pri
Aar
Vik
Riy
Man
37. Write a SQL query to find the location of the alphabet ('a') in the first name
column 'Arav' of the student table.
SELECT STUDENT_ID, FIRST_NAME, INSTR (FIRST_NAME, ‘a’) AS
Position_of_a
FROM Student
WHERE FIRST_NAME = ‘Aarav’;
Output:
STUDENT_ID FIRST_NAME Table Position_of_aHeader
301 Aarav 1
38. Develop an SQL query to retrieve the unique values of MAJOR Subjects
from the Student table and report their length.
SQL query:
SELECT DISTINCT MAJOR, LENGTH(MAJOR) AS MAJOR_LENGTH
FROM Student Table;
Output
MAJOR MAJOR_LENGTH
Data Science 12
Mathematics 11
Biology 7
Chemistry 9
Physics 7
History 7
Data Science 7
39. Create a SQL query that prints FIRST_NAME from the Student table,
replacing 'a' with 'A'.
SQL query:
SELECT REPLACE (FIRST_NAME, ‘a’, ‘A’) AS FIRST_NAME
FROM Student;
Output:
FIRST_NAME
AArAv
DiyA
SiddhArth
PriyA
Aarohi
VikrAm
RiyA
MAnAv
2024-01-15
306 Vikram Rao 9.8 History
10:45:00
42. Create a query to retrieve the last record from the Scholarship table.
SQL query:
SELECT *
FROM ScholarshipTable
ORDER BY SCHOLARSHIP_DATE DESC
LIMIT 1;
Output
Output:
second_highest_gpa
9.6
44. Use the WHERE clause to create a query to view a specific record from the
table
SELECT * FROM Student_Table
WHERE STUDENT_ID = 301;
Output:
STUDENT FIRST_NA LAST_NA GP ENROLLMENT_ MAJ
_ID ME ME A DATE OR
Data
301 Aarav Mehta 8.9 2024-01-15 9:00:00 Scienc
e
45. Create a SQL query to retrieve the number of students with the Major
Subject 'Mathematics’
SQL query:
SELECT COUNT (*) AS number_of_students
FROM Student_Table
WHERE MAJOR = ‘Mathematics’;
Output will be 2.
46. Create a SQL query to retrieve students' full names with GPA >= 8.5 and <=
9.5.
FIRST_NAME LAST_NAME
Aarav Mehta
Diya Kapoor
Priya Verma
Aarohi Sharma
SQL query:
WITH Numbered Rows AS (
SELECT STUDENT_ID, FIRST_NAME, LAST_NAME, GPA,
ENROLLMENT_DATE, MAJOR,
ROW_NUMBER () OVER (ORDER BY STUDENT_ID) AS RowNum
FROM Student
)
SELECT STUDENT_ID, FIRST_NAME, LAST_NAME, GPA,
ENROLLMENT_DATE, MAJOR
FROM NumberedRows
WHERE RowNum % 2 = 1;
Data
301 Aarav Mehta 8.9 2024-01-15 9:00:00 Scienc
e
Biolog
303 Siddharth Joshi 7.2 024-01-15 10:15:00
y
Physic
305 Aarohi Sharma 8.5 2024-01-15 9:30:00
s
STUDENT FIRST_NA LAST_NA GP ENROLLMENT_ MAJ
_ID ME ME A DATE OR
2024-01-15 Englis
307 Riya Patel 9.6
14:30:00 h
47. Create an SQL query to display only odd rows from the Student table.
SQL query:
SELECT *
FROM Student
WHERE MOD (STUDENT_ID, 2) = 0;
Output:
STUDENT FIRST_NA LAST_NA GP ENROLLMENT_
MAJOR
_ID ME ME A DATE
2024-01-15
306 Vikram Rao 9.8 History
10:45:00
48. Create a SQL query to find the nth (say, n=5) highest GPA in a table.
SQL query:
SELECT GPA
FROM (
SELECT GPA, DENSE_RANK () OVER (ORDER BY GPA DESC) AS rank
FROM Student_Table
) AS Ranked
WHERE rank = 5;
Output will be 8.50.
49. Create a SQL query to find the fifth highest GPA without using the LIMIT
keyword.
SQL query:
SELECT GPA
FROM Student_Table s1
WHERE 4 = (SELECT COUNT (DISTINCT s2.GPA)
FROM Student_Table s2
WHERE s2.GPA > s1.GPA);
50. Create a SQL statement that uses a subquery to display the second highest
GPA from a Student table.
SQL query:
SELECT MAX(GPA) AS Second_Highest_GPA
FROM Student
WHERE GPA < (SELECT MAX(GPA) FROM Student);
This result comes from the Student Table, where the second highest GPA is 9.60
belonging to Riya Patel.
51. Write a SQL query to retrieve the MAJOR subjects with fewer than four
persons.
SQL query:
SELECT MAJOR, COUNT(STUDENT_ID) AS Student_Count
FROM StudentTable
GROUP BY MAJOR
HAVING COUNT(STUDENT_ID) < 4;
Output:
MAJOR Student_Count
Data Science 1
Biology 1
Chemistry 1
Physics 1
History 1
English 1
Output:
STUDENT FIRST_NA LAST_NA GP ENROLLMENT_
MAJOR
_ID ME ME A DATE