Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

SQL Questions Answers

Uploaded by

kanishkstudy4
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

SQL Questions Answers

Uploaded by

kanishkstudy4
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Q1.

Create a student table with the student id, name, class, date of birth,
section and marks as attributes where the student id is the primary key.

Ans.

CREATE TABLE student (


student_id INT PRIMARY KEY,
name VARCHAR(50),
class INT,
dob DATE,
section CHAR(1),
marks INT
);

Q2. Insert the details of a new student in the above table.

Ans.

INSERT INTO student (student_id, name, class, dob, section, marks)


VALUES (1, 'Rahul Sharma', 12, '2002-08-15', 'A', 85);

Q3. Delete the details of a particular student whose id is 5, from the above
table.

Ans.

DELETE FROM student WHERE student_id = 5;

Q4. Use the select command to get the details of the students with marks
more than 80.

Ans.

SELECT * FROM student WHERE marks > 80;


Q5. Get the student name and marks, class of the students whose name are
of at least 6 characters long and starting with letter ‘A’.

Ans.

SELECT name, marks, class FROM student


WHERE name LIKE 'A%' AND LENGTH(name) >= 6;

Q6. Display students’ details of all students who are born in the year 2000.

Ans.

SELECT * FROM student WHERE YEAR(dob) = 2000;

Q7. Display students’ names, class, section who are born in month of ‘June’,
also sort them in the descending order of class, within same their names
should display in alphabetical order.

Ans.

SELECT name, class, section FROM student


WHERE MONTH(dob) = 6
ORDER BY class DESC, name;

Q8. Display student id, name and percentage (assuming marks are out of
40).

Ans.

SELECT student_id, name, (marks * 100 / 40) AS percentage FROM student;

Q9. Increase marks of all students by 2%.

Ans.
UPDATE student SET marks = marks + (marks * 2 / 100);

Q10. Add one more column in the above table result, storing values ‘P’ for
pass and ‘F’ for fail.

Ans.

ALTER TABLE student ADD result CHAR(1);


UPDATE student SET result = IF(marks >= 20, 'P', 'F');

Q11. Write SQL commands to create above tables with foreign keys and
primary keys in tables mentioned below:
Order: (order ID (PK), cust_ id (FK), product id (FK) and order Date) by
joining two tables Product: (product id (PK), description, quantity, unit
price) and Customer: (customer ID (PK), customer Name, contact No,
country).

Ans.

CREATE TABLE Product (


product_id VARCHAR(10) PRIMARY KEY,
description VARCHAR(100),
quantity INT,
unit_price INT
);

CREATE TABLE Customer (


customer_id INT PRIMARY KEY,
customer_name VARCHAR(50),
contact_no VARCHAR(15),
country VARCHAR(50)
);

CREATE TABLE Order (


order_id INT PRIMARY KEY,
cust_id INT,
product_id VARCHAR(10),
order_date DATE
);

Q12. Display order ID, customer Name, and order Date by joining two
tables Order and Customer.

Ans.

SELECT order_id, customer_name, order_date


FROM Order, Customer
WHERE Order.cust_id = Customer.customer_id;

Q13. Find the total number of customers from each country along with
country name.

Ans.

SELECT country, COUNT(customer_id) AS customer_count


FROM Customer
GROUP BY country;

Q14. Display order id, customer name, description and unit price of all
orders.

Ans.

SELECT order_id, customer_name, description, unit_price


FROM Order, Customer, Product
WHERE Order.cust_id = Customer.customer_id AND Order.product_id =
Product.product_id;

Q15. Display order date, customer name, unit price for all orders which are
placed after 01-01-2020 in reverse chronological order of order date.
Ans.

SELECT order_date, customer_name, unit_price


FROM Order, Customer, Product
WHERE Order.cust_id = Customer.customer_id AND Order.product_id =
Product.product_id
AND order_date > '2020-01-01'
ORDER BY order_date DESC;

Q16. Display order id and description of products all products ordered from
“India”.

Ans.

SELECT order_id, description


FROM Order, Customer, Product
WHERE Order.cust_id = Customer.customer_id AND Order.product_id =
Product.product_id
AND country = 'India';

Q17. Display no of customers from “India” who had placed the order before
06-01-2020.

Ans.

SELECT COUNT(DISTINCT Customer.customer_id) AS num_customers


FROM Order, Customer
WHERE Order.cust_id = Customer.customer_id AND country = 'India' AND
order_date < '2020-06-01';

Q18. Display total sale made for each product along with description of
products.

Ans.
SELECT product_id, description, SUM(unit_price * quantity) AS total_sale
FROM Product, Order
WHERE Product.product_id = Order.product_id
GROUP BY product_id, description;

Q19. Display order id, product id, unit price and order date of all orders
whose unit price is greater than unit price of product “P01”.

Ans.

SELECT order_id, product_id, unit_price, order_date


FROM Order
WHERE unit_price > (SELECT unit_price FROM Product WHERE product_id =
'P01');

Q20. Display structure of all tables (Order, Product, and Customer) along
with their constraints.

Ans.

DESCRIBE Order;
DESCRIBE Product;
DESCRIBE Customer;

You might also like