0% found this document useful (0 votes)
54 views14 pages

Mysql Questions 2

It has mysql questions.

Uploaded by

goatbale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views14 pages

Mysql Questions 2

It has mysql questions.

Uploaded by

goatbale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

QUESTIONS

Q1 - Write SQL query to create table `fans` as given below with


suitable datatype and constrains.

1. To display the details of fans in descending order of their


DOB.
2. To display the details of FANS who does not belong to AJMER.
3. To count the total number of fans of each fan mode.

Q2 - You are given the following table ‘sales’, write SQL query to
create the table as per the structure given below with proper
datatypes and constrains.
sale_id product_id sale_amount sale_date
1 101 500 2024-01-12
2 102 600 2024-01-13
3 101 700 2024-02-10
4 103 800 2024-02-15
Write a query to retrieve the product that had the highest total
sales amount in February 2024.

Q3 - Consider the following table ‘products’, write SQL query to


create the table as per the structure given below with proper
datatypes and constrains.
product_id product_name price
1 Laptop 1200
2 Mouse 20
3 Keyboard 50
4' Monitor 250
TASK - Write a query to increase the price of every product by
10%, but only for products with a price greater than 100.

Q-4) Write SQL query to create table named `cloth` as given


below with suitable datatype and constrains.

Perform the following operations on the table `cloth`:-


1. Write a query to display cloth names in lower case.
2. Write a query to display the lowest price of the cloths.
3. Write a query to count total number of cloths purchased of
medium size.

Q-5) Given a `logs` table that records login attempts of users,


write SQL query to create the table as per the structure given
below with proper datatypes and constrains.
log_id user_id login_date
1 101 2024-01-15 08:15:00
2 101 2024-01-15 09:45:00
3 102 2024-01-16 10:00:00
4 103 2024-01-17 12:30:00
TASK - Write a query to list date and time as for example “17
January, 2024 | 12:30pm”.

Q6 - Write SQL query to create table named `students` as given


below with suitable datatype and constrains.

Perform the following operations on the table `purchase`:-


1. Display gender wise highest marks.
2. Display city wise lowest marks.
3. Display total number of male and female students.

Q7 - Write SQL query to create table named `employees` as given


below with suitable datatype and constrains.

EMPLOYEE_ID FIRST_NAME EMAIL HIRE_DATE SALARY


100 Steven SKING 1986-05-17 24000.00
101 Neena NKOCHHAR 1987-04-18 17000.00
102 Lex LDEHAAN 1980-06-19 17000.00
103 Alexander AHUNOLD 1980-06-20 9000.00
104 Bruce BERNST 1987-05-21 6000.00
105 David DAUSTIN 1989-05-22 4800.00
106 Valli VPATABAL 1987-01-23 4800.00
107 Diana DLORENTZ 1984-03-24 4200.00
Write a query to list the number of jobs available in the
employees table.

Q8 - Consider table in Q7 and write a query to set email column’s


value with suffix `@gmail.com`.

Q9 – Consider table in Q7 and write SQL query to get name and


50% salary of each employee hired in June of 1980.
ANSWERS
BASIC SETUP
CREATE DATABASE my_db;
USE my_db;
SHOW DATABASES;

ANSWER 1
CREATE TABLE fans (FAN_ID VARCHAR(20), FAN_NAME
VARCHAR(255), FAN_CITY VARCHAR(255), FAN_DOB DATE,
FAN_MODE VARCHAR(90));

INSERT INTO fans VALUES


("F001", "SUSHANT", "MUMBAI", "1998-10-02", "MAIL"),
("F002", "RIYA", "MUMBAI", "1997-12-12", "LETTER"),
("F003", "ANIKA", "DELHI", "2001-06-30", "BLOG"),
("F004", "RUDRA", "AJMER", "2005-08-22", "MAIL"),
("F006", "MIARA", "KOLKATTA", "1998-11-01", "BLOG");
1. SELECT * FROM fans ORDER BY FAN_DOB DESC;

2. SELECT * FROM fans WHERE FAN_CITY != "AJMER";

3. SELECT FAN_MODE, COUNT(*) FROM fans GROUP BY


FAN_MODE;
ANSWER 2
CREATE TABLE sales (sale_id INT PRIMARY KEY, product_id INT,
sale_amount INT, sale_date DATE);

INSERT INTO sales VALUES (1, 101, 500, '2024-01-12'), (2, 102,
600, '2024-01-13'), (3, 101, 700, '2024-02-10'), (4, 103, 800,
'2024-02-15');

SELECT product_id, SUM(sale_amount) FROM SALES WHERE


MONTH(sale_date)=2 AND YEAR(sale_date)=2024 GROUP BY
product_id;

ANSWER 3
CREATE TABLE products (product_id INT PRIMARY KEY,
product_name VARCHAR(255), price INT);

INSERT INTO products VALUES (1, "Laptop", 1200), (2, "Mouse",


20), (3, "Keyboard", 50), (4, "Monitor", 250);

UPDATE products SET price=price+(price*0.1) WHERE price >


100;

ANSWER 4
CREATE TABLE cloth (CCODE VARCHAR(30) PRIMARY KEY, CNAME
VARCHAR(255), SIZE VARCHAR(20), COLOR VARCHAR(40), PRICE
INT, DOP DATE);

INSERT INTO cloth VALUES


("C001", "JEANS", "XL", "BLUE", 990, "2022-01-21"),
("C002", "T SHIRT", "M", "RED", 599, "2021-12-12"),
("C003", "TROUSER", "M", "GREY", 399, "2021-11-10"),
("C004", "SAREE", "FREE", "GREEN", 1200, "2019-11-12"),
("C005", "KURTI", "L", "WHITE", 399, "2021-12-07");

1. SELECT LOWER(CNAME) FROM cloth;

2. SELECT MIN(CPRICE) FROM cloth;


3. SELECT COUNT(*) FROM cloth WHERE SIZE="M";

ANSWER 5
CREATE TABLE logs (log_id INT PRIMARY KEY, user_id INT,
login_date DATETIME);

INSERT INTO logs VALUES (1, 101, "2024-01-15 08:15:00"), (2,


101, "2024-01-15 09:45:00"), (3, 102, "2024-01-16 10:00:00"), (4,
103, "2024-01-17 12:30:00");
SELECT DATE_FORMAT(login_date, "%d %M, %Y | %h:%i %p") as
"DATE TIME" from logs;

ANSWER 6
CREATE TABLE students (`Roll No.` INT PRIMARY KEY, Name
VARCHAR(255), Class VARCHAR(30), Gender VARCHAR(20), City
VARCHAR(255), Marks INT);

INSERT INTO students VALUES (1, "Abhishek", "XI", "M", "Agra",


430), (2, "Prateek", "XII", "M", "Mumbai", 440), (3, "Sneha", "XI",
"F", "Agra", 470), (4, "Nancy", "XII", "F", "Mumbai", 492), (5,
"Himanshu", "XII", "M", "Delhi", 360), (6, "Anchal", "XI", "F",
"Dubai", 256), (7, "Mehar", "X", "F", "Moscow", 324), (8, "Nishant",
"X", "M", "Moscow", 429);
1. SELECT Gender, MAX(Marks) FROM students GROUP BY
Gender;

2. SELECT City, MIN(Marks) FROM students GROUP BY CITY;

3. SELECT Gender, COUNT(*) FROM students GROUP BY


Gender;

ANSWER 7
CREATE TABLE employees (EMPLOYEE_ID INT PRIMARY KEY,
FIRST_NAME VARCHAR(50), EMAIL VARCHAR(50), HIRE_DATE
DATE, SALARY INT);
INSERT INTO employees VALUES
(100, 'Steven', 'SKING', '1986-05-17', 24000.00),
(101, 'Neena', 'NKOCHHAR', '1987-04-18', 17000.00),
(102, 'Lex', 'LDEHAAN', '1980-06-19', 17000.00),
(103, 'Alexander', 'AHUNOLD', '1980-06-20', 9000.00),
(104, 'Bruce', 'BERNST', '1987-05-21', 6000.00),
(105, 'David', 'DAUSTIN', '1989-05-22', 4800.00),
(106, 'Valli', 'VPATABAL', '1987-01-23', 4800.00),
(107, 'Diana', 'DLORENTZ', '1984-03-24', 4200.00);

ANSWER 8
UPDATE employees SET EMAIL=CONCAT(EMAIL, "@gmail.com");
BEFORE
AFTER

ANSWER 9
SELECT FIRST_NAME, SALARY*0.5 FROM employees WHERE
MONTH(HIRE_DATE)=6 AND YEAR(HIRE_DATE)=1980;

You might also like