SQL Assignment Saurav

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

QUESTION:-

CODE:-
CREATE DATABASE sql_assignment;
USE sql_assignment;

CREATE TABLE Bike203(


ModelID INT,
Manufacturer VARCHAR(100),
M_Year YEAR,
Price INT,
Sales INT,
Showroom_ID INT
);

INSERT INTO
Bike203(ModelID,Manufacturer,M_Year,Price,Sales,Showroom_ID)
VALUES
(101,"Bajaj",2010,95000,50,1),
(101,"Bajaj",2012,90000,30,2),
(102,"Bajaj",2020,130000,60,3),
(201,"TVS",1995,105000,70,3),
(201,"TVS",1995,105000,70,4),
(202,"TVS",2020,125000,20,5),
(301,"Royal Enfield",1960,145000,45,1),
(301,"Royal Enfield",1960,145000,45,5),
(302,"Royal Enfield",2000,180000,70,2),
(401,"Hero",2000,65000,80,1),
(401,"Hero",2000,65000,80,2),
(402,"Hero",2024,55000,90,3),
(402,"Hero",2023,55000,90,4),
(501,"Yamaha",2006,115000,10,1),
(502,"Yamaha",2020,140000,25,3);

SELECT * FROM Bike203;


CODE:-
CREATE TABLE Manufacturer203(
Model_ID INT PRIMARY KEY,
Model_Name VARCHAR(100),
Manufacturing_Place VARCHAR(100),
Engine_CC INT
);

INSERT INTO Manufacturer203(Model_ID,Model_Name,Manufacturing_Place,Engine_CC)


VALUES
(101,"Pulsar 150","Maharashtra",150),
(102,"Pulsar NS200","Maharashtra",200),
(201,"Apache RTR 160","Tamil_Nadu",160),
(202,"Apache RTR 200","Tamil_Nadu",200),
(301,"Bullet 350","Tamil_Nadu",350),
(302,"Classic 350","Tamil_Nadu",350),
(401,"Splendor Plus","Haryana",100),
(402,"HF Deluxe","Haryana",100),
(501,"FZ S FI","Tamil_Nadu",150),
(502,"MT-15","Tamil_Nadu",160);

SELECT * FROM Manufacturer203;


CODE :-

CREATE TABLE Showroom203(


Showroom_ID INT PRIMARY KEY,
Showroom_Name VARCHAR(50)
);

INSERT INTO Showroom203(Showroom_ID,Showroom_Name)


VALUES
(1,"Bengaluru"),
(2,"Mysuru"),
(3,"Hubballi"),
(4,"Mangaluru"),
(5,"Belagavi");

SELECT * FROM Showroom203;


CODE:-
SELECT Model_Name,M_Year FROM Bike203
LEFT JOIN Manufacturer203
ON Bike203.Model_ID = Manufacturer203.Model_ID
WHERE bike203.M_Year = 2020;

CODE:-
SELECT Manufacturer, AVG(Price) FROM Bike203
GROUP BY Manufacturer;
CODE:-

SELECT Showroom_Name, SUM(Sales) FROM Bike203


JOIN Showroom203
ON Bike203.Showroom_ID=Showroom203.Showroom_ID
GROUP BY Showroom_Name
ORDER BY sum(sales) DESC
LIMIT 1;

CODE:-
SELECT Manufacturer, SUM(sales) FROM Bike203
JOIN Showroom203
ON Bike203.Showroom_ID = Showroom203.Showroom_ID
GROUP BY Manufacturer;
CODE:-
SELECT Manufacturer203.Model_Name, Bike203.Manufacturer, Bike203.Price
FROM Bike203
JOIN Showroom203
ON Bike203.Showroom_ID = Showroom203.Showroom_ID
JOIN Manufacturer203
ON Bike203.ModelID = Manufacturer203.Model_ID
WHERE Bike203.Price = (SELECT MAX(Price) FROM Bike203);
CODE:-
SELECT Manufacturer203.Model_Name, Bike203.Manufacturer, Bike203.Price
FROM Bike203
JOIN Manufacturer203
ON Bike203.ModelID = Manufacturer203.Model_ID
WHERE Bike203.Price > (SELECT AVG(Price) FROM Bike203);

CODE:-
SELECT DISTINCT Showroom203.Showroom_Name, Bike203.Manufacturer
FROM Bike203
JOIN Showroom203
ON Bike203.Showroom_ID = Showroom203.Showroom_ID
WHERE Bike203.Manufacturer = 'TVS';
CODE:-
SELECT Showroom203.Showroom_Name, AVG(Bike203.Price) AS Average_Price
FROM Bike203
JOIN Showroom203
ON Bike203.Showroom_ID = Showroom203.Showroom_ID
GROUP BY Showroom203.Showroom_Name;

CODE:-
SELECT Model_Name, Manufacturing_Place
FROM Manufacturer203
WHERE Manufacturing_Place = 'Tamil_Nadu';
CODE:-
SELECT DISTINCT Showroom203.Showroom_Name, Manufacturer203.Model_Name,
Manufacturer203.Engine_CC
FROM Bike203
JOIN Manufacturer203
ON Bike203.ModelID = Manufacturer203.Model_ID
JOIN Showroom203
ON Bike203.Showroom_ID = Showroom203.Showroom_ID
WHERE Manufacturer203.Engine_CC > 200;

CODE:-
SELECT Manufacturer203.Model_Name, SUM(Bike203.Sales) AS Total_Sales
FROM Bike203
JOIN Manufacturer203
ON Bike203.ModelID = Manufacturer203.Model_ID
GROUP BY Manufacturer203.Model_Name;
CODE:-
SELECT Showroom203.Showroom_Name, Bike203.Price
FROM Bike203
JOIN Showroom203
ON Bike203.Showroom_ID = Showroom203.Showroom_ID
WHERE Bike203.Price = (SELECT MIN(Price) FROM Bike203);
CODE:-
SELECT Manufacturer203.Manufacturing_Place, Manufacturer203.Model_Name,
AVG(Manufacturer203.Engine_CC) AS Avg_Engine_CC
FROM Manufacturer203
GROUP BY Manufacturer203.Manufacturing_Place, Manufacturer203.Model_Name;
CODE:-
SELECT Manufacturer203.Model_Name, Manufacturer203.Engine_CC, Bike203.Sales
FROM Bike203
JOIN Manufacturer203 ON Bike203.ModelID = Manufacturer203.Model_ID
WHERE Manufacturer203.Engine_CC > 150 AND Bike203.Sales > 50;

CODE:-
SELECT Showroom203.Showroom_Name, AVG(Bike203.Price) AS Avg_Price
FROM Bike203
JOIN Showroom203
ON Bike203.Showroom_ID = Showroom203.Showroom_ID
GROUP BY Showroom203.Showroom_Name;

You might also like