ASSESSEMENTS SQL - Batch 10

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

ASSESSEMENT SQL 1

DATE: 09/May/2023

BATCH 10

ASSESSMENT 1

1. Write a query to create the table with the below-mentioned fields in SQL. Where the
First_name ,DOB,Honour_subject shouldn’t allow any Null value.
Create table samp(First_name varchar(20) not null, DOB date not null,
Honour_subject varchar(20) not null)
2. Write a query to insert 10 random data into the student table. With DOB Ranging
from 1/Jan1995 to 31/Dec/1999 and Student marks from 1 to 100
3. Write a Query to pick students who Don’t have a Last name

Select * from Student where Last_name = null

4. Define Union vs Union All.

UNION:
Union is simply joining two or more data sets into a single set. This is used to combine
two queries into a single result set using the select statements. Union extracts all the rows
that are described in the query.

UNION ALL:
Column count in all tables must be the same. The data types of columns must be the same.
The columns must be in the same order in each table.

5. Write a Query to Pick find average and total mark scored by each Honor subject.

6. Write a Query to Pick students whose First name contains at least of 5 characters
and second last character is ‘a’

SELECT FIRST_NAME AS STUDENT NAME FROM STUDENT WHERE


FIRST_NAME LIKE '%A'
7. Write a Query to change the Honor subject name from ‘Maths’ to ‘Mathematics’

8. Define ACID compliance.


Atomicity:
A transaction is atomic, means that wither it happens in its entirely or none of it at all.
Consistency:
The DB moves from one consistent state from another consistent state.
Isolation:
the resource allocation to the transaction happens such that the transaction gets the
impression that it is the only transaction happening in isolation.
Durability:
All changes made to the database during transaction are permanent and that accounts for
the durability of the transaction.

9. Write a Query to add gender column and insert a value into the column.

#ALTER TABLE STUDENT ADD GENDER VARCHAR(10);


#update student set GENDER ='female' where FIRST_NAME like '%a';
#update student set GENDER ='male' where FIRST_NAME not like '%a';
12. Write a Query to convert the student ID column to Primary Key

alter table add constraint pk-101 primary key (student ID)

13. a) State the difference between Where and Having?

• WHERE is used to filter rows before grouping


• HAVING is used to exclude records after grouping

b) State the difference between Delete, Drop and Truncate


• The DELETE command deletes one or more existing records from the table in the database.
• The DROP Command drops the complete table from the database.
• The TRUNCATE Command deletes all the rows from the existing table, leaving the row with the
column names.

14. Write the Query to pick Honor subject by highest of average Score grouped across
honor subject.
SELECT TOP 1 HONUR_SUBJECT, AVG(STU_MARKS) FROM STUDENT GROUP BY
HONOUR_SUBJECT ORDER BY AVG(STU_MARKS) DESC
SQL ASSESSMENT 2

DATE: 13-05-2023

BATCH 10

1. How can you select all the even number records from a table?

SELECT * FROM Employee WHERE E_Code %2=0

2. As like ‘TOP n’ Clause, write a query to Pick the Bottom 5 records from practice Employee Table.

SELECT TOP 5 * FROM Employee ORDER BY E_Code DESC;

3. Write a SQL query to find the 3th highest employee salary from an employee table by each
department.

SELECT department, max(salary) as '3rd highest salary' FROM Employee WHERE salary < (SELECT
max(salary) FROM employee WHERE department=department) GROUP BY department HAVING
Count(salary) >= 3;
4. With this below table, write a query to delete the duplicate records. e.g. (first and last row are
duplicated)

DELETE FROM test WHERE column_A IN ( SELECT column_A FROM test GROUP BY column_A,
column_B HAVING COUNT(*) > 1

5. With the result from above table write a query to update column A as inverse of Column B as
like example.

UPDATE test SET column_A = 1 - column_b WHERE column_B IN (0, 1)


select * from test

6. With this three below table we used for practice


Dim_Students Fact_marks dim_subject

Write a Query to de-normalize this tables, and pick only columns as below and filter only rows
where student name starts with any character between ‘a’ and ‘h’ and may follow by any
characters.

SELECT s.student_id, s.student_name, c.subject_name, m.marks FROM students s JOIN marks m


ON s.subject_id = m.subject_id JOIN courses c ON c.course_id = m.course_id WHERE
s.student_name LIKE '[a-h]%'

output

7. Create this below table Write a Query to count the son or daughter for each parent.

create table family


(
id int,
parent_id int,
name varchar(50)
)

insert into family


values(1,null,'Ashwin'),(2,1,'Madhu'),(3,1,'Ravi'),(4,2,'Ram'),(5,3,'Seenu'),
(6,3,'Sonu')
select * from family SELECT p.Name AS Name, COUNT(C.id) AS num_child FROM family c JOIN family p
ON c.parent_id = p.id WHERE c.parent_id IS NOT NULL GROUP BY p.name;

8. From the same Family table pick the grand parent of Sonu.

SELECT grandparent.name AS grand_parent FROM family AS parent JOIN family AS grandparent


ON parent.parent_id = grandparent.id JOIN family AS child ON child.parent_id =parent.id
WHERE child.name = 'Sonu'
SQL Final Assessment
Date :
Batch 10
Phase : Scenario 50 Marks
1. Create this below tables

CREATE TABLE Customers (


CustomerID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
City VARCHAR(50)
);

CREATE TABLE Orders (


OrderID INT PRIMARY KEY,
CustomerID INT,
OrderDate DATE,
TotalAmount DECIMAL(10,2),
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);

INSERT INTO Customers (CustomerID, FirstName, LastName, City)


VALUES
(1, 'John', 'Doe', 'New York'),
(2, 'Jane', 'Smith', 'Los Angeles'),
(3, 'Mike', 'Johnson', 'Chicago'),
(4, 'Sarah', 'Williams', 'Houston');

INSERT INTO Orders (OrderID, CustomerID, OrderDate, TotalAmount)


VALUES
(1, 1, '2023-05-01', 100.50),
(2, 1, '2023-05-03', 75.20),
(3, 2, '2023-05-02', 150.00),
(4, 3, '2023-05-04', 200.75),
(5, 4, '2023-05-05', 50.30),
(6, 2, '2023-05-06', 125.80),
(7, 3, '2023-05-07', 90.50),
(8, 4, '2023-05-08', 175.25),
(9, 1, '2023-05-09', 110.00),
(10, 2, '2023-05-10', 85.75);

Write a Query to pick customer complete name whose total purchase was higher than
average purchases of whole customers.
Select concat(c.firstname, ' ', c.lastname) as fullname, sum(o.totalamount) as
totalpurchaseamount from customers c join orders o on c.customerid = o.customerid
Group by c.customerid, c.firstname, c.lastname having sum(o.totalamount) > ( select
avg(totalamount) from orders
)

Output:

2. Using the below table


CREATE TABLE employeedetail (
Firstname VARCHAR(50),
Lastname VARCHAR(50),
Salery INT,
Joiningdate DATETIME,
Department VARCHAR(10),
Gender VARCHAR(7),
employeeid INT IDENTITY(1,1)
);

CREATE TABLE projectdetail (


Employeedetailid INT,
Projectname VARCHAR(50),
projectdetailid INT IDENTITY(1,1)
);

INSERT INTO employeedetail (Firstname, Lastname, Salery, Joiningdate, Department, Gender)


VALUES
('vikas', 'Ahlawat', 600000, '2013-02-15T11:16:28.290', 'IT', 'Male'),
('Nikita', 'Jain', 530000, '2014-01-09T17:31:07.793', 'HR', 'Female'),
('Ashish', 'Kumar', 1000000, '2014-01-09T10:05:07.793', 'IT', 'Male'),
('Nikhil', 'Sharma', 480000, '2014-01-09T09:00:07.793', 'HR', 'Male'),
('Anish', 'Kadian', 5000000, '2014-01-09T09:31:07.793', 'Pyroll', 'Male'),
('Aakash', 'Raj', 2000000, '2013-02-15T08:32:00.347', 'IT', 'Male');

INSERT INTO projectdetail (Employeedetailid, Projectname)


VALUES
(1, 'Task Track'),
(1, 'CLP'),
(1, 'Survey Management'),
(2, 'HR Management'),
(3, 'Task Track'),
(3, 'GRS'),
(3, 'DDS'),
(4, 'HR Management'),
(6, 'GL Management');

Using the above table


Write a query to pick employee who is not assigned to any project
Select employeedetail.*from employeedetail left join projectdetail on
employeedetail.employeeid = projectdetail.employeedetailid where
projectdetail.employeedetailid is null;

Write a query to pick project which is not assigned to any employee

Select projectdetail.* from projectdetail left join employeedetail on


projectdetail.employeedetailid = employeedetail.employeeid where
employeedetail.employeeid is null;

3. Using the below table

CREATE TABLE TemperatureData (


Id INT,
RecordDate DATE,
Temperature INT
);

INSERT INTO TemperatureData (Id, RecordDate, Temperature)


VALUES
(1, '2015-01-01', 10),
(2, '2015-01-02', 25),
(3, '2015-01-03', 20),
(4, '2015-01-04', 30);

Write a Query to pick days by which temperature was higher than the previous day.
Select t1.recorddate, t1.temperature as presentday_temperature, t2.temperature as
Lastday_temperature from temperaturedata t1 join temperaturedata t2 on t1.id = t2.id + 1 where
t1.temperature > t2.temperature;
Output:

4. Write a SQL query to find the percentage of males and females. Print the output to the
nearest integer.
INPUT: Gender

Gender
Male
Male
Male
Female
Male
Female
OUTPUT:

Male% Female%
67 33
Select gender, round(count(*) * 100.0 / (select count(*) from test), 0) as percentage from test
group by gender;
5. Use the practise Employee table and create the below table

Classify all employees based on the total payout without using case- when..

OUTPUT:

You might also like