SQL Class 2 Assignment 2 Solution
SQL Class 2 Assignment 2 Solution
Q. Create a table named "Students" with the following columns: StudentID (int),
ills
FirstName (varchar), LastName (varchar), and Age (int). Insert at least three records into
the table.
Sk
StudentID int PRIMARY KEY,
FirstName varchar(50),
LastName varchar(50),
Age int
);
a
at
INSERT INTO Students (StudentID, FirstName, LastName, Age)
VALUES
D
Q. Update the age of the student with StudentID 1 to 21. Delete the student with
StudentID 3 from the "Students" table.
A. UPDATE Students
G
SET Age = 21
WHERE StudentID = 1;
WHERE StudentID = 3;
Q. Retrieve the first names and ages of all students who are older than 20.
FROM Students
ills
WHERE Age <18 :
Q. Create a table named "Customers" with the following columns and constraints:
Sk
CustomerID (int) as the primary key.
);
G
Q. You have a table named "Orders" with columns: OrderID (int), CustomerID (int),
OrderDate (date), and TotalAmount (decimal). Create a foreign key constraint on the
"CustomerID" column referencing the "Customers" table.
REFERENCES Customers(CustomerID);
Q. Create a table named "Employees" with columns:
Salary (decimal) check constraint to ensure salary is between 20000 and 100000.
ills
EmployeeID int PRIMARY KEY,
Sk
Salary decimal CHECK (Salary BETWEEN 20000 AND 100000)
);
a
Q. Create a table named "Books" with columns:
);
G
FROM Employees
FROM Employees
Q. Given a table named "Products" with columns: ProductID, ProductName, Price, and
InStock (0- for out of stock, 1- for in stock). Write an SQL query to retrieve the product
ills
names and prices of products that are either priced above $100 or are out of stock.
FROM Products
Sk
WHERE Price > 100 OR InStock = 0;
Q. Using the "Products" table, write an SQL query to retrieve the product names and
prices of products that are in stock and priced between 50 and 150.
a
A. SELECT ProductName, Price
at
FROM Products
orders placed by customer ID 1001 after January 1, 2023, or orders with a total amount
exceeding $500.
ro
FROM Orders
WHERE CustomerID = 1001 AND OrderDate > '2023-01-01' OR TotalAmount > 500;
G
Q. Retrieve the ProductName of products from the "Products" table that have a price
between $50 and $100.
A. SELECT ProductName
FROM Products
A.SELECT Name
FROM Employees
ills
Q. Retrieve the names of customers from the "Customers" table who are not from the
city 'New York' or 'Los Angeles'.
A.SELECT Name
Sk
FROM Customers
a
Q. Retrieve the names of employees from the "Employees" table who are either from the
"HR" department and have an age less than 30, or they are from the "Finance"
at
department and have an age greater than or equal to 35.
A.SELECT Name
D
FROM Employees
WHERE (Department = 'HR' AND Age < 30) OR (Department = 'Finance' AND Age >=
35);
w
Q. Retrieve the names of customers from the "Customers" table who are not from the
ro
city 'London' and either have a postal code starting with '1' or their country is not 'USA'.
A. SELECT Name
FROM Customers
G
WHERE City <> 'London' AND (PostalCode LIKE '1%' OR Country <> 'USA');