SQL Assignment
1Q. Create the following table named "Student" and write SQL queries for
the tasks that follow:
Table Structure and Sample Data:
CREATE TABLE Student (
STUDENT_ID INT,
FIRST_NAME VARCHAR(50),
LAST_NAME VARCHAR(50),
GPA DECIMAL(4,2),
ENROLLMENT_DATE DATETIME,
MAJOR VARCHAR(100)
);
-- Sample Data Insertion
INSERT INTO Student (STUDENT_ID, FIRST_NAME, LAST_NAME, GPA,
ENROLLMENT_DATE, MAJOR) VALUES
(201, 'Shivansh', 'Mahajan', 8.79, '2021-09-01 09:30', 'Computer
Science'),
(202, 'Umesh', 'Sharma', 8.44, '2021-09-01 08:30', 'Mathematics'),
(203, 'Rakesh', 'Kumar', 5.6, '2021-09-01 10:00', 'Biology'),
(204, 'Radha', 'Sharma', 9.2, '2021-09-01 12:45', 'Chemistry'),
(205, 'Kush', 'Kumar', 7.85, '2021-09-01 08:30', 'Physics'),
(206, 'Prem', 'Chopra', 9.56, '2021-09-01 09:24', 'History'),
(207, 'Pankaj', 'Vats', 9.78, '2021-09-01 02:00', 'English'),
(208, 'Navleen', 'Kaur', 7.0, '2021-09-01 06:30', 'Mathematics');
1. Write a SQL query to fetch "FIRST_NAME" from the Student table in upper
case and use ALIAS name as STUDENT_NAME.
SELECT UPPER(FIRST_NAME) AS STUDENT_NAME
FROM Student;
Output:
SHIVANSH
UMESH
RAKESH
RADHA
KUSH
PREM
PANKAJ
NAVLEEN
2. Write a SQL query to fetch unique values of MAJOR Subjects from Student
table.
SELECT DISTINCT MAJOR
FROM Student;
Output:
Computer Science
Mathematics
Biology
Chemistry
Physics
History
English
3. Write a SQL query to print the first 3 characters of FIRST_NAME from Student
table.
SELECT SUBSTRING(FIRST_NAME, 1, 3) AS FIRST3
FROM Student;
Output:
Shi
Ume
Rak
Rad
Kus
Pre
Pan
Nav
4. Write a SQL query to find the position of alphabet ('a') in the first name
column 'Shivansh' from Student table.
SELECT POSITION('a' IN FIRST_NAME) AS POSITION_OF_A
FROM Student
WHERE FIRST_NAME = 'Shivansh';
Output:
5