SQL All Questions
SQL All Questions
2. There is a Student table that has two column IDs and names. Answer We need to update the
name of the student with id = 10 as John. Which of the following query is correct?
a) UPDATE STUDENT SET NAME"John" WHERE ID-10;
b) UPDATE NAME FROM STUDENT SET NAME =" John" WHERE ID-10;
c) UPDATE NAME ="John" FROM STUDENT WHERE ID=10;
d) None of the mentioned.
3. Assume that you work for a company that operates an e-commerce website. You are
tasked with updating the order status for a particular customer's orders. You are provided
with the given table schema:
name VARCHAR(50),
customer_ id INT,
status VARCHAR(20) );
Which SQL statement can be used to update the status of all the orders for the customer
with the email john@example.com to "shipped?
a) UPDATE orders SET status ='shipped' WHERE id IN ( SELECT id FROM customer WHERE
email = 'john@example.com');
b) UPDATE orders o JOIN customers c ON o.customer_id = c.id SET o.status ='shipped'
WHERE c.email = 'john@example.com';
4) Which SQL query retrieves information about the primary key columns of the "customers table?
a) SELECT CONSTRAINT_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_NAME ='customers " AND CONSTRAINT_NAME = 'PRIMARY:
6) Assume you are a database administrator for a company that is updating an existing SQL database
with new product data. The new data requires a change in the data type for the price column in the
product table from decimal to an integer. Which SQL command should you use to change the data
type?
a) MODIFY ALTER COLUMN
b) ALTER COLUMN
c) CHANGE ALTER COLUMN
d) ALTER UPDATE COLUMN
7) You want to allow records to be inserted with a blank email ID field initially. However, you need to
ensure that this blank field is automatically populated with a random email ID ending in
@gmail.com' after insertion
Which combination of constraints and mechanisms should you use to achieve this?
a)CHECK
b)DEFAULT
c) NOT NULL
d) UNIQUE '@gmail.com'
8) Assume that your company has a database that stores information about employees and their
salaries. The HR department needs to update the salary of a specific employee who has just received
a raise
Which DML command should the HR admin use to update the salary of the employee with
employee_id 101?
a) UPDATE employees SET salary = 75000 WHERE employee_id =101;
b) DELETE FROM employees WHERE employee_id =101;
c) SELECT * FROM employees WHERE employee_id = 101;
d) INSERT INTO employees (employee_id, first_name, last_name, salary) VALUES (101, 'John', 'Doe',
75000);
9) Assume that you are working as a database administrator for a company that has recentiy
implemented a new payroll system. As part of this system, you need to create a new table named
'employees' that will store information about all the company's employees. The employees table
should have the given columns:
emp_id (integer, auto-incremented, primary key)
first_name (string)
last_name (string)
job_title (string)
hire_date (date)
salary (decimal)
Which SQL statement can be used to create the "employees" table with the required columns?
Analyze the given choices and select the correct option.
a) CREATE TABLE employees ( emp_id SERIAL PRIMARY KEY, first_name TEXT, last_ name TEXT,
job_title TEXT, hire_date DATE, salary NUMERIC (10,2) );
b) CREATE TABLE employees( emp_id INT PRIMARY KEY, first_name TEXT, last_ name TEXT, job_title
TEXT, hire_date DATE, salary NUMERIC (10,2) );
C) CREATE TABLE employees ( emp_id INT PRIMARY KEY AUTO_INCREMENT, first_name
VARCHAR(255), last_name VARCHAR(255), job_title VARCHAR(255), hire date DATE, salary
DECIMAL (10, 2) );
D) CREATE TABLE employees ( emp id INTEGER PRIMARY KEY, first_name VARCHAR(255), last_name
VARCHAR (255) , job_title VARCHAR(255), hire date DATE, salary DECIMAL (10,2) );
10) Which of the following is the correct syntax to join two tables in a MS SQL server database?
a) select * from Table_1 ON Table_2 INNER JOIN Table_1.Id = Table_2.Id;
b) select * from Table_1 INNER JOIN Table_2 ON Table_1.Id = Table_2.Id;
c) select * from Table_1 AND Table_2 INNER JOIN ON Table_1.Id = Table_2.Id;
d) None of the Above
11) You have a table named ‘employees’ with the following columns:
‘employee_id’, ‘first_name’, ‘last_name’, ‘salary’, and ‘department_id’:
You are tasked with updating the salary of the employees in the ‘Finance’ department to reflect a
10% salary increase.
Which SQL UPDATE statement accomplishes this task?
a) UPDATE employees SET salary = salary *1.10 WHERE department_id = (SELECT department_id
FROM employees WHERE department_id = ‘Finance’);
b) UPDATE employees SET salary = salary *1.10 WHERE department_id = ‘Finance’;
c) UPDATE employees SET salary = salary *1.10 WHERE employee_id IN (SELECT employee_id FROM
employees WHERE department_id =’Finance’);
d) UPDATE employees SET salary = salary*1.10 WHERE department_id IN (SELECT department_id
FROM employees WHERE department_id = ‘Finance’);
12) As a payroll manager for a tech company with employees in different locations, how can you use
SQL code to calculate the average salary of the employees in the IT department from the
payroll_table who are located in a specific location , for example ,”XYZ”?
Analyze the given choices and select the correct option.
a) SELECT AVG() AS average_salary
FROM payroll_table
department = ‘IT’ AND location =’xyz’;
b) SELECT AVG(salary) AS average_salary
FROM payroll_table
WHERE department = ‘IT’ AND location =’xyz’;
c) SELECT AVG(salary) AS average_salary
FROM payroll_table_
WHERE department = ‘IT’ AND location =’xyz’;
d) SELECT COUNT(*) / SUM (salary) FROM employee_data;
13) Consider that you have a table called ‘Orders’ with the columns ‘OrderID’, ‘CustomerID’,
‘OrderDate’ and ‘Order Total’.
You need to write a query that will return all the orders placed in the year 2023, sorted by order
date in descending order.
Which query can you use to fulfil your requirement? Analyze the given choices and select the correct
option
a) SELECT * FROM Orders WHERE YEAR(OrderDate) = 2023 ORDER BY OrderDate
b) SELECT * FROM Orders WHERE YEAR(OrderDate) = 2023 ORDER BY CustomerID DESC
c) SELECT * FROM Orders WHERE YEAR(OrderDate) = 2023 ORDER BY OrderDate DESC
d) SELECT * FROM Orders WHERE YEAR (OrderDate) = 2023 ORDER BY CustomerID ASC
14) Assume that you are asked to create a new table for the customer data of an online store. The
table should have columns for the customer’s name, email address and phone number.
Which SQL command should you use to create the table in this scenario?
Select the correct option from the given choices.
a) CREATE
b) ALTER
c) INSERT
d) UPDATE
15) Assume that company has two tables one table contains customer information and other table
contain other information , How will you write SQL queries to integrate the two tables and retrieve
total amount spent by each other?
16) Consider that Nick is assigned a task to isolate all students in between age group of 18 to 30
years at his school. He has a table of his school named Student in the database with column ID
Name, Email and Age in order to ensure that age column and contains values between 18 and 30
which constraint should nick use?
a) CREATE INDEX
b) PRIMARY KEY
c) CHECK
d) FOREIGN KEY
(a) ALTER TABLE Student ADD CONSTRAINT CHK_Age _Range ( AGE between 18 AND 30)**FROM
GPT
17) In a large e-commerce you have been asked with retrieving list of 5 customers who have made
the most distinct product purchases of the year 2021 from order details table you can use the
following query?
a) SELECT TOP 5 customer_id, COUNT(DISTINCT product_id) AS distinct_purchases
FROM order_details
WHERE YEAR(order_date) = 2021
GROUP BY customer_id
ORDER BY distinct_purchases DESC
b) SELECT customer_id, COUNT(DISTINCT product_id) AS distinct_purchases
FROM order_details
WHERE YEAR(order_date) = 2021
GROUP BY customer_id
ORDER BY distinct_purchases ASC
c) SELECT DISTINCT TOP 5 customer_id, COUNT(product_id) AS distinct_purchases
FROM order_details
WHERE YEAR(order_date) = 2021
GROUP BY customer_id
ORDER BY distinct_purchases DESC
d) UNKNOWN
((SELECT customer_id, COUNT(DISTINCT product_id) AS distinct_product_count
FROM order_details
WHERE YEAR(Order_date) = 2021
GROUP BY customer_id
ORDER BY distinct_product_count DESC
LIMIT 5; -- Corrected the LIMIT clause)))- students wrote this in notebook
18) Assume you are maintaining existing DB which is used to store customer data for retailing
company. New requirement has been added i.e delete column that is not longer needed?
(no other options available)
a) DROP COLUMN
19) Given a rank & display new row (question incomplete)
a) SELECT * , rank() OVER (Order BY total_score)
abc FROM student;
20) In E-R diagram, entity address is example of what type of attribute? (no options available)
a) In an E-R (Entity-Relationship) diagram, the entity "address" is an example of a composite
attribute. (from BARD)
21) In relational database , what is purpose of primary key? (no options available)
a) ensuring uniqueness of a column
22) Which of the following situation does not require the need of FROM clause on SELECT
statement?
a) using inner join
b) using full outer join
c) using across join
d) using derived tables
e) using only items listed either literals/vars/arithmetic expression
23) Which of the following describes (2NF) in Database normalisation? (no options available)
a) The option "It eliminates fields that do not depend on the key" is not entirely correct in
describing 2NF. (FROM BARD)
24) SQL query to retrieve names of employee who have salary greater than average salary in their
department. (no options)
a) SELECT name FROM employees
WHERE salary > (SELECT AVG(salary) FROM employee); (wrong query written)
25) what is the purpose of cross join?
a) Returns Cartesian product of 2 or more tables
26) Which must be true about table to be in (1NF)?
a) Should only have single (atomic values attributes/columns)
27)
CODING QUESTIONS:
1) Mysql :Most Active Users on Social Media
Environment Specifications & Instructions
Type of Database MySQL
Database Name to be used SocialData
Problem Statement
Write a SQL query to find the top 4 most active users on a social media platform, where activity is
defined as the maximum number of posts created. The query should include a column called the
user_id as userld, the username as userName and no_of_oost as topPost that indicates the user who
created the maximum number of posts.
Return the output result based on topPost in descending order.
Column Name: userld, userName, topPost
ANSWER:
use SocialData;
SELECT U.USER_ID AS userId, U.USERNAME AS userName, P.NO_OF_POST AS topPost
FROM USERS U JOIN POSTS P ON U.USER_ID=P.USER_ID
ORDER BY P.NO_OF_POST DESC LIMIT 4;
ANSWER:
Use ComapnyDb;
SELECT D.DEPARTMENT_ID, D.DEPARTMENT_NAME, A.FIRST_NAME, A.LAST_NAME, A.SALARY
FROM DEPARTMENTS D LEFT JOIN
(SELECT E.FIRST_NAME, E.LAST_NAME, E.DEPARTMENT_ID, S.SALARY
FROM EMPLOYEES E JOIN SALARIES S ON E.EMPLOYEE_ID = S.EMPLOYEE_ID) A
ON D.DEPARTMENT_ID = A.DEPARTMENT_ID
ORDER BY D.DEPARTMENT_ID;