0% found this document useful (0 votes)
20 views28 pages

Queries

The document provides a comprehensive set of SQL queries related to various database operations, including data retrieval, aggregation, and manipulation on tables like SALESMAN, Vehicle, Student, and Scholarship. It includes examples of using mathematical, string, date functions, and aggregate functions, along with creating and modifying tables. Each query is accompanied by a brief description of its purpose and expected output.

Uploaded by

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

Queries

The document provides a comprehensive set of SQL queries related to various database operations, including data retrieval, aggregation, and manipulation on tables like SALESMAN, Vehicle, Student, and Scholarship. It includes examples of using mathematical, string, date functions, and aggregate functions, along with creating and modifying tables. Each query is accompanied by a brief description of its purpose and expected output.

Uploaded by

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

Database query using SQL (Mathematical, string,


Date and time functions in SQL)

Consider table SALESMAN with following data:

SNO SNAME SALARY BONUS DATEOFJOIN


A01 Beena Mehta 30000 45.23 2019-10-29
A02 K. L. Sahay 50000 25.34 2018-03-13
B03 Nisha Thakkar 30000 35.00 2017-03-18
B04 Leela Yadav 80000 NULL 2018-12-31
C05 Gautam Gola 20000 NULL 1989-01-23
C06 Trapti Garg 70000 12.37 1987-06-15
D07 Neena Sharma 50000 27.89 1999-03-18

1) Display Salesman name, bonus after rounding off to zero decimal


places.
Select SNAME, round (BONUS,0) from SALESMAN;
2) Display name, total salary of all salesman after addition of salary
and bonus and truncate it to 1 decimal places.

Select sname, truncate((SALARY+BONUS),1) from SALESMAN;

3) Display remainder of salary and bonus of Salesman whose SNO


starting with ‘A’
Select MOD(SALARY,BONUS) from SALESMAN where SNO like ’A%’;
4) Display position of occurrence of string “ta” in salesmen name.
Select sname, instr (Sname,” ta”) from SALESMAN;

5) Display four characters from salesman name starting from second


character.
Select sname, substr(Sname,2,4) from SALESMAN;
6) Display last 5 characters of name of SALESMAN.
Select sname, right (Sname,5) from SALESMAN;

7) Display details of salesman whose name containing 10 characters.


Select * from salesman where length(sname)=10;
8) Display month name for the date of join of salesman
Select DATEOFJOIN, monthname(DATEOFJOIN) from SALESMAN;

9) Display current date and day of the year of current date.


Select date (now ()), Dayo year (date (now ())) from dual;

10) Display name of the weekday for the DATEOFJOIN of


SALESMAN;
Select DATEOFJOIN, dayname(DATEOFJOIN) from SALESMAN;
11) Display SNO, name of the youngest SALESMAN.
Select sno, sname, dateofjoin from salesman where dateofjoin= (select max
(DATEOFJOIN) from SALESMAN);

12) Display name and salary of the oldest SALESMAN.


Select sname, salary, dateofjoin from salesman where dateofjoin=(select
min(dateofjoin) from salesman);
❖ Database query using SQL (Aggregate functions, Group by, order
by query in SQL)

Consider the following table Vehicle:


V_no Type Company Price Qty
AW125 Wagon Maruti 250000 25
J00083 Jeep Mahindra 4000000 15
S9090 SUV Mistubishi 2500000 18
M0892 Mini Van Datsun 1500000 26
W9760 SUV Maruti 2500000 18
R2409 Mini Van Mahindra 350000 15

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;

15 . Display total price of all types of vehicles.


Select Type, sum (Price* Qty) from Vehicle group by Type;

16 Display the details of the vehicles having maximum price.


Select * from vehicle where price= (select max(price) from vehicle);
17 Display total vehicles of Maruti company.
Select company, sum(qty) from vehicle group by company having
company='Maruti';

18 Display average price of all type of vehicles.


Select type, avg(price) from vehicle group by type;
19) Display type and minimum price of each vehicle company.
Select type, company, in(price) from vehicle group by company;

20 Display minimum, maximum, total and average price of Mahindra


company vehicles.
Select company, min(price), max(price), sum(price), avg(price) from vehicle where
company='Mahindra';
21 Display details of all vehicles in ascending order of their price.
Select * from vehicle order by price asc;

22 Display details of all vehicles in ascending order of type and descending


order of vehicle number.
Select * from vehicle order by type asc, v_no desc;
23. Create a student table with the student id, name, and marks as
attributes where the student id is the primary key. Ans.:
create table student
(student int (3) primary key, name
varchar (20) not null, marks decimal
(5,2));

24.Insert the details of a new student in the above table.


25 . Delete the details of a student in the above table.
Ans.:
delete from student where student=5;

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;

29 Write a SQL query to order the (student ID, marks) table in


descending order of the marks. Ans.:
select * from student order by marks desc;
30 Write a SQL query to display the marks without decimal places,
display the reminder after diving marks by 3 and display the square
of marks.
Ans.:
select round(marks,0), mod(marks,3), pow(marks,2) from
student;

31 Write a SQL query to display names into capital letters, small


letters, display first 3 letters of name, display last 3 letters of name,
display the position the letter A in name
Ans.:
select ucase(name), lcase(name), left(name,3), right(name,3), instr(name,'a') from
student;
32 Remove extra spaces from left, right and both sides from the text - "
Informatics Practices Class XII " Ans.:
select ltrim (" Informatics Practices Class XII ") "Left Spaces", rtim ("
Informatics Practices Class XII ") "Right
Trim", trim (" Informatics Practices Class XII ");

33 Display today's date in "Date/Month/Year" format Ans.:


select concat (date (now ()), concat ("/”, concat (month (now ()), concat ("/”, year
(now ())))));
34 Display dayname, monthname, day, dayname, day of month, day of year for
today's date
Ans.:
select dayname (now ()), monthname (now ()), day (now ()), dayname (now ()),
dayofmonth (now ()), dayofyear (now ());
Student Table
STUDENT_I FIRST_NAM LAST_NAM GP ENROLLMENT_DAT
MAJOR
D E E A E

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

303 Siddharth Joshi 7.2 024-01-15 10:15:00 Biology

304 Priya Verma 9.45 2024-01-15 11:00:00 Chemistry

305 Aarohi Sharma 8.5 2024-01-15 9:30:00 Physics

306 Vikram Rao 9.8 2024-01-15 10:45:00 History

307 Riya Patel 9.6 2024-01-15 14:30:00 English

Mathematic
308 Manav Gupta 7.85 2024-01-15 7:15:00
s

Program Table

STUDENT_REF_ID PROGRAM_NAME PROGRAM_START_DATE

301 Data Science 2024-01-15 0:00:00

302 Mathematics 2024-01-15 0:00:00

308 Mathematics 2024-01-15 0:00:00

305 Physics 2024-01-15 0:00:00

304 Chemistry 2024-01-15 0:00:00

307 Psychology 2024-01-15 0:00:00

306 History 2024-01-15 0:00:00

303 Biology 2024-01-15 0:00:00


Scholarship Table
STUDENT_REF_ID SCHOLARSHIP_AMOUNT Scholarship Table

301 5500 2022-02-10 0:00:00

302 4800 2022-03-18 0:00:00

303 3200 2022-05-25 0:00:00

304 4600 2022-06-15 0:00:00

305 5200 2022-04-20 0:00:00

306 6000 2022-07-10 0:00:00

307 5300 2022-08-18 0:00:00

308 4000 2022-09-25 0:00:00

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

40. Create a SQL query to print information about students whose


FIRST_NAME ends with 'a' and contains six alphabets.
SQL query:
SELECT STUDENT_ID, FIRST_NAME, LAST_NAME, GPA,
ENROLLMENT_DATE, MAJOR
FROM Student_Table
WHERE FIRST_NAME LIKE ‘_____a’
AND LENGTH(FIRST_NAME) = 6;
Output will be none
41. Build a SQL query to print information about students with GPAs between
9.00 and 9.99.
SQL query:
SELECT STUDENT_ID, FIRST_NAME, LAST_NAME, GPA,
ENROLLMENT_DATE, MAJOR
FROM Student Table
WHERE GPA BETWEEN 9.00 AND 9.99;
Output
STUDENT FIRST_NA LAST_NA GP ENROLLMENT_ MAJO
_ID ME ME A DATE R

9.4 2024-01-15 Chemis


304 Priya Verma
5 11:00:00 try

2024-01-15
306 Vikram Rao 9.8 History
10:45:00

307 Riya Patel 9.6 2024-01-15 English

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

308 4000 2022-09-25 0:00:00


43. Write an SQL query to retrieve the second-highest value of an integer field
from the student table.
SELECT MAX(GPA) AS second_highest_gpa
FROM StudentTable
WHERE GPA < (SELECT MAX(GPA) FROM StudentTable);

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;

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

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

8.7 2024-01-15 Mathema


302 Diya Kapoor
5 8:45:00 tics

9.4 2024-01-15 Chemistr


304 Priya Verma
5 11:00:00 y

2024-01-15
306 Vikram Rao 9.8 History
10:45:00

7.8 2024-01-15 Mathema


308 Manav Gupta
5 7:15:00 tics

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);

Output: The fifth highest GPA is 8.75

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

52. Create an SQL query to display the last record in a table


SQL query:
SELECT *
FROM StudentTable
ORDER BY STUDENT_ID DESC

Output:
STUDENT FIRST_NA LAST_NA GP ENROLLMENT_
MAJOR
_ID ME ME A DATE

7.8 2024-01-15 Mathema


308 Manav Gupta
5 7:15:00 tics

You might also like