SQL_QUESTIONS
SQL_QUESTIONS
The given database consists of two tables: Customer and Prod_Order, which store customer details and their
respective orders. The Customers table includes fields such as CID, FirstName, LastName, Email and City. Each row
represents a unique customer, identified by the CID, which acts as the primary key. This table helps in maintaining
customer-related information, including their contact details and location.
The Prod_Order table records order transactions made by customers. It contains fields such as PID, CID,
ProductName, Quantity, OrderDate, and TotalPrice. The PID is the primary key, uniquely identifying each order,
while CID serves as a foreign key, establishing a relationship between the two tables. This connection enables
tracking which customer placed a particular order. The OrderDate column captures when the order was made,
and TotalPrice reflects the cost of the order.
These two tables are related through the CID, allowing us to perform various SQL operations, such as retrieving
customer details along with their orders using JOINs, filtering orders based on specific conditions using the
WHERE clause, and analyzing sales trends using aggregate functions like SUM, COUNT, and AVG. Queries can also
be used to sort order records by date or price, update existing data, or delete unwanted records. With these
tables, we can perform a wide range of SQL operations, from basic data retrieval to complex queries, helping us
gain meaningful insights from the dataset.
→ QUESTIONS ON PROJECTION, WHERE, ALIAS OR SELECT CLAUSE.
1. WAQTD ALL THE RECORDS OF THE CUSTOMERS TABLE.
3. WAQTD Select the first names and last names of all customers.
4. Select only the PROD_NAME and TOTALPRICE from the PROD_ORDER table.
22. Select only EMAIL and CITY from the CUSTOMER table.
23. Retrieve PID, CUSTOMERID, and ORDERDATE from the PROD_ORDER table.
27. Display only the ORDERDATE column from the PROD_ORDER table.
40. Display the FIRSTNAME, EMAIL, and PHONE of customers from 'San Diego'.
61. Get customers with first name 'John' AND last name 'Doe'.
62. Find products ordered in July 2023 AND with a total price greater than 500.
63. Retrieve all orders where the quantity is greater than 1 AND the total price is above 100.
64. Get customers with email containing 'email.com' AND living in Chicago.
65. Find products ordered by customer ID 4 AND with a price below 500.
66. Retrieve all orders placed in 2021 AND costing more than 200.
69. Get customers whose first name starts with 'J' AND live in Texas (Dallas or Houston).
71. Get products with total price greater than 700 OR ordered in 2022.
72. Find customers whose first name is 'David' OR last name is 'Clark'.
76. Retrieve orders with quantity more than 2 OR total price above 500.
77. Find orders placed in 2021 OR 2023.
78. Get customers with first names starting with 'A' OR living in Phoenix.
84. Get customers NOT living in New York, Los Angeles, or Chicago.
90. Retrieve customers from New York OR Los Angeles, but NOT from Chicago.
91. Get products ordered in 2022 AND costing more than 100, OR ordered in 2023.
92. Find customers whose first name starts with 'J' AND NOT living in San Jose.
93. Retrieve orders with a quantity greater than 2 AND NOT made in 2021.
94. Get customers who are from Chicago OR Houston AND have an email ending with '@email.com'.
95. Find orders made in 2023 AND total price NOT less than 500.
96. Retrieve customers who are NOT from New York AND have a last name 'Smith' OR 'Johnson'.
97. Find all orders with product names NOT containing 'Wireless' AND ordered in 2022.
98. Get orders with a total price NOT greater than 500 OR ordered in 2021.
99. Retrieve customers whose first name starts with 'A' OR 'J' AND NOT living in Dallas.
100. Get orders made in 2023 AND NOT containing 'Wireless' in the product name.
101. Find customers who are from Los Angeles OR San Diego AND have an email ending with '@email.com'.
102. Retrieve all orders made in 2022 OR having a quantity greater than 3.
103. Get customers NOT from Phoenix AND having a last name 'Davis' OR 'Lewis'.
104. Find orders where the product name is 'Laptop' OR 'Smartphone' AND NOT ordered in 2021.
105. Retrieve customers whose email contains 'gmail' OR 'yahoo' AND NOT living in Chicago.
106. Get all orders placed by customer ID 5 AND NOT containing 'Charger' in the product name.
107. Find products ordered in 2021 OR 2022 AND NOT having a quantity less than 2.
108. Retrieve orders with a total price greater than 100 AND NOT made in December.
109. Get customers living in San Antonio OR Dallas AND NOT having 'martinez' in their email.
110. Find all orders where the total price is below 300 AND NOT placed in 2023.
111. Retrieve products ordered by customer ID 10 AND NOT containing 'Graphics' in the product name.
112. Get customers from Houston OR Philadelphia AND NOT having an email with '.edu'.
113. Find all orders placed before July 2021 OR having a total price above 700.
114. Retrieve customers whose first name starts with 'D' AND NOT from San Diego.
116. Find products that have a total price between 200 and 500 AND NOT ordered in 2023.
117. Retrieve customers whose last name starts with 'A' OR 'B' AND NOT from San Jose.
118. Get all orders placed in the first quarter of any year (Jan-Mar) AND NOT by customer ID 6.
119. Find customers with an email ending in '@email.com' AND NOT having 'smith' in their last name.
120. Retrieve all orders with a quantity greater than 2 OR total price below 100 AND NOT ordered in 2021.
121. Get customers from New York OR Chicago AND NOT having an email with 'brown'.
122. Find orders where the product name contains 'Drive' OR 'Router' AND NOT ordered in 2022.
123. Retrieve customers whose first name contains 'R' OR 'M' AND NOT from Los Angeles.
124. Get all orders where the product name contains 'VR' OR 'Monitor' AND NOT having a total price above 500.
125. Find customers who have an email ending with '@email.com' AND NOT living in San Antonio.
126. Retrieve orders made in August OR November AND NOT placed by customer ID 7.
127. Get products ordered by customer ID 8 OR 9 AND NOT costing more than 600.
128. Find customers who have a last name ending in 'N' OR 'S' AND NOT from Houston.
129. Retrieve orders with a total price above 200 OR quantity below 3 AND NOT made in 2022.
➔ Special Operators (IN, BETWEEN, LIKE, EXISTS, ANY, ALL, IS NULL, etc.)
130. Retrieve all customers from New York, Los Angeles, or Chicago.
133. Find all customers whose first name starts with 'J'.
137. Retrieve orders where the total price is higher than all orders placed by customer ID 7.
138. Get all customers who have placed at least one order.
141. Retrieve all orders with a NULL customer ID (if any exist).
142. Retrieve all customers who are from Texas or California (assuming more data).
143. Find all orders where the product name starts with 'S'.
147. Retrieve orders where the product name contains either 'LAPTOP' or 'HEADSET'.
152. Get all customers where last name does not contain 'N'.
154. Retrieve orders where the total price is higher than the average total price.
156. Find all orders where total price is less than 500.
157. Retrieve customers whose city is not 'DALLAS'.
161. Get all customers where email does not contain 'JOHN'.
164. Get all orders where the total price is not equal to 799.99.
165. Retrieve customers whose city name is less than 7 characters long.
174. Get all orders where order date is later than '01-JAN-21'.
176. Find all products where quantity is less than or equal to 10.
177. Get all orders where order date is not '15-FEB-21'.
180. Calculate the total cost of each order (quantity * total price per unit).
181. Find the remaining amount a customer needs to reach $1000 in total purchase.
182. Increase each order’s total price by 5% due to tax.
185. Compute the difference between the highest and lowest order price.
186. Find orders where the quantity is an even number.
196. Generate a username using first name and last name in lowercase.
197. Create an invoice label with customer and order details.
201. Find orders where the total price is greater than the average total price.
202. Retrieve orders where the quantity is greater than any order placed by customer ID 2.
206. Get all orders where total price is less than all orders by customer ID 5.
207. Find customers who have ordered a specific product (e.g., 'LAPTOP').
208. Retrieve orders where the quantity matches the maximum quantity ordered.
209. Get orders where the total price is higher than at least one order by CID 3.
213. Retrieve customers who have spent more than $500 in total.
218. Find orders where the quantity is equal to the average quantity.
219. Retrieve all orders placed on the same day as the most recent order.
237. Find the total revenue for each city based on customer orders.
239. Find customers who have spent more than $500 in total.
240. Retrieve products that generated more than $1000 in total sales.
241. Find cities where total customer spending is above $2000.
243. Find product categories where the average order value is greater than $300.
246. Find customers from the city where the first order was placed.
247. Get the details of the most expensive product ordered.
248. Find orders where the total price is higher than the average total price.
249. Find customers who spent more than the average order value.
250. Retrieve products that cost more than the cheapest product ordered.
251. Find customers who have placed at least one order before a specific date.
252. List customers whose first order amount is more than the minimum order amount.
253. Find products that have been sold for more than their average price.
254. Get the order details for the highest total order price.
255. Find the customers whose total spending is greater than $500.
257. Find the average order value for each customer and display customers whose average order value is above
$200.
261. Retrieve customers whose total quantity of ordered products exceeds 10.
262. Find customers who have placed at least one order and are from a city where total spending is greater than
$5000.
263. Find customers who placed an order with the largest quantity.
264. Retrieve orders for the product with the lowest price.
265. Find customers who have spent more than the highest single order value.
266. Get the details of the customer who placed the first order.
267. Find products that were ordered by the customer who placed the highest quantity order.
272. List customers and the total amount they have spent.
273. Find customers who have spent more than the average total spending of all customers.
276. List all products along with the customers who bought them.
278. Find products that have been ordered by more than one customer.
279. Find customers who ordered a product that costs more than the average product price.
280. Find customers who have never ordered a "Laptop".
285. List all products along with the number of unique customers who ordered them.
289. Retrieve customers who have returned and placed a new order after at least 6 months of inactivity.
291. Retrieve customers who ordered a product with a price above the average.
292. List customers along with the number of different years they have placed orders in.
293. Retrieve customers who have ordered only during a specific year (e.g., 2022).
294. Find customers who have made a purchase in both the first and second half of the year.
298. Fetch customers from 'New York' who have placed an order.
299. Retrieve all unique cities where customers have placed orders.
300. List customers who have purchased a 'LAPTOP'.
301. Fetch all orders placed on '05-JUL-23' along with customer details.
303. List customers who have purchased more than one product.
304. Retrieve customer details along with the sum of their total purchases.
305. Find customers who have ordered both a 'LAPTOP' and a 'SMARTPHONE'.
308. Retrieve customers who have placed an order for more than 2 items in a single order.
313. Retrieve pairs of customers who have the same last name.
314. Find customers who live in different cities but have the same first name.
315. Retrieve customers who have the same first letter in their first name.
316. Find customers who have similar names (first two letters match).
317. Retrieve customers who have the same last name but different first names.
318.Identify customers who have the same last two letters in their first names.
321. Get all customers and the number of orders they placed (including those with no orders).
322. List all customers along with the most expensive product they purchased (if any).
323. Find customers who have placed an order for more than one product.
324. List customers who placed orders before they registered in the system.
325. Retrieve customers who have purchased more than one distinct product.
326. Retrieve customers along with the most expensive product they have purchased (if any).
328. Retrieve the current date and time from the system.
331. Retrieve customers sorted by their last names and assign a row number to each.
332. Retrieve the system-generated row ID for each customer in the CUSTOMER table.
333. Display the first and last customer IDs in the table.
334. Retrieve orders placed within the last 7 days using SYSDATE.
335. Retrieve the most recent order based on system-generated row numbers.
336. Get all customer details sorted by the latest added entry using ROWID.
➔ SOME OTHER CLAUSE USED FOR THE CREATION OF THE TABLE, DELETE, UPDATE ETC.
338. To create a table of prod_order table with their associated attributes and connection with the customer table.
339. Insertion of the records in the customer table which is created already.
340. Insertion of the records in the existing prod_order table.