0% found this document useful (0 votes)
4 views16 pages

SQL Assignment Solutions

The document provides SQL assignment solutions, including the creation of various tables with primary keys, performing DML operations, and executing queries with aggregate functions. It covers topics such as integrity constraints, views, joins, and string operations. Additionally, it demonstrates user creation and permission granting in SQL.

Uploaded by

Arif choudhary
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)
4 views16 pages

SQL Assignment Solutions

The document provides SQL assignment solutions, including the creation of various tables with primary keys, performing DML operations, and executing queries with aggregate functions. It covers topics such as integrity constraints, views, joins, and string operations. Additionally, it demonstrates user creation and permission granting in SQL.

Uploaded by

Arif choudhary
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/ 16

SQL Assignment Solutions

1. Create Employee and Course tables with a primary key in each.


CREATE TABLE Employee (
EmpID INT PRIMARY KEY,
EmpName VARCHAR(50),
Department VARCHAR(50)
);

CREATE TABLE Course (


CourseID INT PRIMARY KEY,
CourseName VARCHAR(50)
);
SQL Assignment Solutions

2. Create a table and perform all DML commands


CREATE TABLE Sample (
ID INT PRIMARY KEY,
Name VARCHAR(50)
);

INSERT INTO Sample VALUES (1, 'Alice');


UPDATE Sample SET Name = 'Alicia' WHERE ID = 1;
DELETE FROM Sample WHERE ID = 1;
SELECT * FROM Sample;
SQL Assignment Solutions

3. Create a department table with all integrity constraints.


CREATE TABLE Department (
DeptID INT PRIMARY KEY,
DeptName VARCHAR(50) UNIQUE NOT NULL,
Location VARCHAR(50),
Budget DECIMAL(10, 2) CHECK (Budget > 0)
);
SQL Assignment Solutions

4. Queries on the Client table


-- a
SELECT * FROM Client WHERE BalDue BETWEEN 1000 AND 2000;

-- b
SELECT MIN(BalDue), MAX(BalDue) FROM Client;

-- c
SELECT Name, City, State FROM Client WHERE State != 'Maharashtra';
SQL Assignment Solutions

5. Create any two views and retrieve data from them


CREATE VIEW ViewClient AS
SELECT Name, City FROM Client;

CREATE VIEW ViewDue AS


SELECT Name, BalDue FROM Client WHERE BalDue > 1000;

-- Retrieve
SELECT * FROM ViewClient;
SELECT * FROM ViewDue;
SQL Assignment Solutions

6. Create a Product table and perform any two DML commands


CREATE TABLE Product (
ProductID INT PRIMARY KEY,
ProductName VARCHAR(50),
Price DECIMAL(10, 2)
);

INSERT INTO Product VALUES (1, 'Pen', 10.00);


UPDATE Product SET Price = 12.00 WHERE ProductID = 1;
SQL Assignment Solutions

7. Perform queries using aggregate functions


SELECT COUNT(*) AS TotalClients FROM Client;
SELECT AVG(BalDue) AS AverageDue FROM Client;
SQL Assignment Solutions

8. Run queries
-- a
SELECT * FROM Client WHERE City LIKE 'M%';

-- b
SELECT * FROM Client WHERE State = 'Maharashtra' AND BalDue > 10000;
SQL Assignment Solutions

9. Perform updates and queries


-- a
UPDATE Client SET City = 'Bangalore', PinCode = '570098', State = 'Karnataka' WHERE
Client_No = 'C0005';

-- b
SELECT * FROM Sales WHERE YEAR(PurchaseDate) = 2018;
SQL Assignment Solutions

10. Create Student and Furniture tables with a primary key


CREATE TABLE Student (
StudentID INT PRIMARY KEY,
StudentName VARCHAR(50)
);

CREATE TABLE Furniture (


FurnitureID INT PRIMARY KEY,
FurnitureName VARCHAR(50)
);
SQL Assignment Solutions

11. Perform any two DML operations on the student table


INSERT INTO Student VALUES (1, 'John');
UPDATE Student SET StudentName = 'Johnny' WHERE StudentID = 1;
SQL Assignment Solutions

12. Show one example of primary key and foreign key relationship
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
EmpID INT,
FOREIGN KEY (EmpID) REFERENCES Employee(EmpID)
);
SQL Assignment Solutions

13. Perform any 5 string operations


SELECT UPPER('hello') AS UpperCase;
SELECT LOWER('WORLD') AS LowerCase;
SELECT LENGTH('Test') AS Length;
SELECT SUBSTRING('Database', 1, 4) AS SubStr;
SELECT CONCAT('Hello', ' ', 'World') AS FullStr;
SQL Assignment Solutions

14. Create a new user, grant permissions on a table to the user, and verify the granted permissions
CREATE USER new_user IDENTIFIED BY 'password';
GRANT SELECT, INSERT ON Product TO new_user;
SHOW GRANTS FOR new_user;
SQL Assignment Solutions

15. Perform Join Operations


-- Right Outer Join
SELECT * FROM Employee RIGHT OUTER JOIN Department ON Employee.Department =
Department.DeptName;

-- Left Outer Join


SELECT * FROM Employee LEFT OUTER JOIN Department ON Employee.Department =
Department.DeptName;
SQL Assignment Solutions

16. Create 2 tables and insert 7 values in each


CREATE TABLE Table1 (
ID INT PRIMARY KEY,
Name VARCHAR(50)
);

CREATE TABLE Table2 (


ID INT PRIMARY KEY,
Description VARCHAR(50)
);

INSERT INTO Table1 VALUES (1,'A'),(2,'B'),(3,'C'),(4,'D'),(5,'E'),(6,'F'),(7,'G');


INSERT INTO Table2 VALUES (1,'X'),(2,'Y'),(3,'Z'),(4,'W'),(5,'P'),(6,'Q'),(7,'R');

You might also like