0% found this document useful (0 votes)
14 views11 pages

SQL All Questions

The document presents a series of SQL-related questions and options, covering topics such as querying employee data, updating records, creating tables, and understanding database normalization. It includes multiple-choice questions with various SQL commands and scenarios for database management. The questions address practical SQL applications for managing employee and customer data in a relational database context.

Uploaded by

Darshan Patil
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)
14 views11 pages

SQL All Questions

The document presents a series of SQL-related questions and options, covering topics such as querying employee data, updating records, creating tables, and understanding database normalization. It includes multiple-choice questions with various SQL commands and scenarios for database management. The questions address practical SQL applications for managing employee and customer data in a relational database context.

Uploaded by

Darshan Patil
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/ 11

1. You are working with a database containing employee information.

You want to find employees


who are either in the Engineering department and have a salary greater than $80,000, or employees
in the Sales department with a Senior job title.
Which SQL query should you use to achieve this?
options
a) SELECT * FROM employees WHERE (department 'Engineering' AND salary > 80000) OR
(department Sales' AND job_title 'Senior');
b) SELECT * FROM employees WHERE (department - 'Engineering' OR department 'Sales') AND
(salary > 80000 OR job_title - 'Senior');
c) SELECT FROM employees WHERE (department - 'Engineering' AND salary > 80000) AND
(department - 'Sales' AND job title = 'Senior' ); Help
d) SELECT * FROM employees WHERE (department = Engineering AND salary > 80000) XOR
(department = 'Sales' AND job title = Senior' );

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:

CREATE TABLE customers (

id INT PRIMARY KEY

name VARCHAR(50),

email VARCHAR (50) UNIQUE

CREATE TABLE orders (

id INT PRIMARY KEY,

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';

c) UPDATE orders WHERE customer_id =( SELECT id FROM customers WHERE


email='john@example.com') SET status ='shipped';

d) UPDATE orders o SET status ='shipped' WHERE c.email ='john@example.com' JOIN


customers c ON o.customer_id = c.id ;

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:

b) SELECT CONSTRAINT_NAME, COLUMN_NAME


FROM INFORMATION_SCHEMA. KEY COLUMN_USAGE
WHERE REFERENCED_TABLE_NAME ='customers' AND CONSTRAINT_TYPE = 'PRIMARY KEY':

c) SELECT COLUMN_NAME, CONSTRAINT_NAME


FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN USAGE
WHERE TABLE_NAME = 'customers' AND CONSTRAINT_TYPE = 'PRIMARY KEY';

d) SELECT COLUMN_NAME, CONSTRAINT_NAME


FROM INFORMATION _SCHEMA.CONSTRAINT_COLUMN USAGE
WHERE REFERENCED_TABLE_NAME = 'customers' AND CONSTRAINT_TYPE = 'PRIMARY';
5) Consider a scenario where a company possesses two tables: the first one holds customer details
with columns such as customer id, customer name, and email and the second one contains order
information with columns like order_id, customer_id, total_amount, and order date Describe how
you would formulate a SQL query to combine these tables and obtain the total amount spent by
each customer
a) SELECT customer.customer_id, SUM(total_amount) AS total_spent
FROM Customer
INNER J0IN order ON customer_id = order.customer_id
GROUP BY customer_id;

b) SELECT customer_id, SUM(order_amount) AS total_spent


FROM customer
INNER JOIN order ON customer_id = order.customer_id
GROUP BY customer.customer_id;

c) SELECT customer.customer_id, SUM(order.total amount) AS total_spent


FROM customer
INNER JOIN order ON customer.customer_id = order.customer_id
GROUP BY customer.customer_id;
d) no option available

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?

a) SELECT customer.customer_id, SUM(total_amount) AS total_spent


FROM customer
INNER JOIN order ON customer_id = order.customer_id
GROUP BY customer_id
b) SELECT customer_id, SUM(order_amount) AS total_spent
FROM customer
INNER JOIN order ON customer_id = order.customer_id
GROUP BY customer.customer_id
c) SELECT customer.customer_id, SUM(order.total_amount) AS total_spent
FROM customer
INNER JOIN order ON customer.customer_id = order.customer_id
GROUP BY customer.customer_id
d) UNKNOWN

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;

2) MySQL : Employee Data


Environment Specifications & Instructions
Type of Database MySQL
Database Name to be used EmployeeData
Problem Statement
Write a standard SQL query that will return the value of "EMPID" and "EMPNAME" for those
employees who earn more than their managers. Additionally, the query will return the salary of an
employee as column name "EMPSALARY" and the salary of a manager as column name
"MANAGERSALARY".
The rows should be returned in the increasing order of "EMPID" Column Name: EMPID EMPNAME,
EMPSALARY, MANAGERSALARY
ANSWER:
Use EmployeeData;
select E.EMPID, E.EMPNAME, E.SALARY AS EMPSALARY, M.SALARY AS MANAGERSALARY
from TBLEMPLOYEE E join TBLEMPLOYEE M on E.MANAGERID = M.EMPID AND E.SALARY >
M.SALARY ORDER BY EMPID;
ALTERNATE SOLUTION
select e1.EMPID, e1.EMPNAME, e1.SALARY as EMPSALARY, e2.SALARY AS MANAGERSALARY
from tblemployee e1, tblemployee e2
where e1.MANAGERID = e2.EMPID and e1.SALARY > e2.SALARY order by e1.EMPID;

3) MySQL :Employee with Highest Salary


Environment Specifications & Instructions
Type of Database MySQL
Database Name to be used CompanyDb
Problem Statement
Write a SQL query to retrieve a list of department_id, department_name, and the first_name & the
last_name of the employee with their salary in each department. If a department has no employees
or salary information, it should still appear in the result with NULL values. The rows should be
returned in the increasing order of "department id" Column Name: department_ id,
department_name, first name, last _name, salary.

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;

4)MySQL : Course Fees


Environment Specifications & Instructions
Type of Database MySQL
Database Name to be used: DB_Institute
Existing Information ID " Database Name to be used: DB_Institute
TBL_Students Enroll is described below.
Problem Statement
Construct a query that displays StudentID, Name, DateOfEnrollment, FeesPaid, FeesDue, and
PaymentDate of students who have not paid fees for the python course.
The result should be in the order of the students name
Column Name: StudentID, Name, DateOfEnrollment,FeesPaid, FeesDue,PaymentDate
ANSWER:
Use DB_Institute;
SELECT E.STUDENTID AS StudentID, E.STUDENTNM AS Name, E.DTENROLL AS DateOfEnrollment,
F.FEESPAID AS FeesPaid, F.FEESDUE AS FeesDue, F.PAYMENTDATE AS PaymentDate FROM
(SELECT * FROM TBL_STUDENTS_ENROLL WHERE STUDENTID NOT IN
(SELECT E.STUDENTID
FROM
TBL_STUDENTS_ENROLL E JOIN
TBL_STUDENTS_FEES F ON
E.STUDENTID=F.STUDENTID
AND E.COURSENM=’PYTHON’) AND COURSENM=’PYTHON’) E
LEFT JOIN
TBL_STUDENTS_FEES F
ON E.STUDENTID= F.STUDENTID
ORDER BY E.STUDENTNM
5) MySQL : Order placed by Customer
Environment Specifications & Instructions
Type of Database MySQL
Database Name to be used: Customerdb
Problem Statement: Write a SQL query to find the customer who has placed more than two orders
with a total amount exceeding $500. Display the customer_id and the total number of orders placed
by that customer as total_orders. The rows should be returned in the increasing order of "customer
id". Column Name: customer_id, total _orders
ANSWER:
Use Customerdb;
SELECT A.CUSTOMER_ID, B.TOTAL_ORDERS FROM
(SELECT CUSTOMER_ID, COUNT(ORDER_ID) AS TOTAL_ORDERS
FROM ORDERS WHERE TOTAL_AMOUNT > 500.00
GROUP BY CUSTOMER_ID) A JOIN
(
SELECT CUSTOMER_ID, COUNT(ORDER_ID) AS TOTAL_ORDERS
FROM ORDERS
GROUP BY CUSTOMER_ID
)B
ON A.CUSTOMER_ID =B.CUSTOMER_ID AND A.TOTAL_ORDERS>=2
ORDER BY A.CUSTOMER_ID;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy