presudo 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,728 @@
1
+ Filtering and Unique Values
2
+
3
+
4
+ 1. Data Filtering: Highest Marks in Cities
5
+
6
+ In this case, we'll first filter out the highest marks in each city.
7
+
8
+ Step-by-Step Process:
9
+
10
+ We will:
11
+
12
+ Filter students by city and find the highest marks.
13
+
14
+ Use MAX() to get the highest marks for each city.
15
+
16
+ Group the data by city to ensure the highest marks are calculated per city.
17
+
18
+ SQL Query for Highest Marks in Each City (Data Filtering):
19
+ SELECT city, MAX(marks) AS highest_marks
20
+ FROM students
21
+ GROUP BY city;
22
+
23
+ Explanation:
24
+
25
+ MAX(marks): Finds the highest marks in each city.
26
+
27
+ GROUP BY city: Groups the students by their cities to get the highest marks for each city individually.
28
+
29
+ 2. Unique Value: Highest Marks in Cities
30
+
31
+ Now, to find the unique student(s) who have achieved the highest marks in each city, we need to perform a unique value extraction.
32
+
33
+ We will:
34
+
35
+ Find the highest marks in each city using a subquery.
36
+
37
+ Retrieve the students whose marks match those highest marks, ensuring that the students with the highest marks are unique per city.
38
+
39
+ SQL Query for Unique Students with Highest Marks:
40
+ SELECT s.city, s.name, s.marks
41
+ FROM students s
42
+ WHERE s.marks = (
43
+ SELECT MAX(marks)
44
+ FROM students
45
+ WHERE city = s.city
46
+ );
47
+
48
+ Explanation:
49
+
50
+ The subquery (SELECT MAX(marks) FROM students WHERE city = s.city) calculates the highest marks for each city.
51
+
52
+ The outer query filters students in each city whose marks match the highest marks in that city.
53
+
54
+ This will ensure that the unique student(s) with the highest marks in each city are listed.
55
+
56
+ 3. Combining Data Filtering and Unique Values: Highest Marks in Cities
57
+
58
+ We can combine both filtering and unique value extraction by ensuring that the highest marks per city are unique to each student.
59
+
60
+ SQL Query for Highest Marks with Unique Values (Data Filtering + Uniqueness):
61
+ SELECT s.city, s.name, s.marks
62
+ FROM students s
63
+ JOIN (
64
+ SELECT city, MAX(marks) AS highest_marks
65
+ FROM students
66
+ GROUP BY city
67
+ ) AS max_marks
68
+ ON s.city = max_marks.city
69
+ AND s.marks = max_marks.highest_marks;
70
+
71
+ -------------------------------------------------------------------------------------------------------------------------------------
72
+
73
+ 1. Using GROUP BY and MAX()
74
+ SELECT city, MAX(marks) AS highest_marks
75
+ FROM students
76
+ GROUP BY city;
77
+
78
+
79
+ Explanation: This is correct, and there's no typo here.
80
+
81
+ 2. Using a Subquery for Highest Marks
82
+ SELECT s.city, s.name, s.marks
83
+ FROM students s
84
+ WHERE s.marks = (
85
+ SELECT MAX(marks)
86
+ FROM students
87
+ WHERE city = s.city
88
+ );
89
+
90
+
91
+ Explanation: No typos detected here either. The query is correctly written.
92
+
93
+ 3. Using JOIN with a Subquery
94
+ SELECT s.city, s.name, s.marks
95
+ FROM students s
96
+ JOIN (
97
+ SELECT city, MAX(marks) AS highest_marks
98
+ FROM students
99
+ GROUP BY city
100
+ ) AS max_marks
101
+ ON s.city = max_marks.city
102
+ AND s.marks = max_marks.highest_marks;
103
+
104
+
105
+ Explanation: This query looks good too. The syntax is correct.
106
+
107
+ 4. Using ROW_NUMBER() Window Function
108
+ WITH RankedStudents AS (
109
+ SELECT city, name, marks,
110
+ ROW_NUMBER() OVER (PARTITION BY city ORDER BY marks DESC) AS rank
111
+ FROM students
112
+ )
113
+ SELECT city, name, marks
114
+ FROM RankedStudents
115
+ WHERE rank = 1;
116
+
117
+
118
+ Explanation: No typos here either. It’s correct for retrieving students with the highest marks in each city.
119
+
120
+ 5. Using RANK() for Tied Results
121
+ WITH RankedStudents AS (
122
+ SELECT city, name, marks,
123
+ RANK() OVER (PARTITION BY city ORDER BY marks DESC) AS rank
124
+ FROM students
125
+ )
126
+ SELECT city, name, marks
127
+ FROM RankedStudents
128
+ WHERE rank = 1;
129
+
130
+
131
+ Explanation: Everything is in order, and the query is correctly written.
132
+
133
+ 6. Using DENSE_RANK() to Handle Tied Marks
134
+ WITH RankedStudents AS (
135
+ SELECT city, name, marks,
136
+ DENSE_RANK() OVER (PARTITION BY city ORDER BY marks DESC) AS rank
137
+ FROM students
138
+ )
139
+ SELECT city, name, marks
140
+ FROM RankedStudents
141
+ WHERE rank = 1;
142
+
143
+
144
+ Explanation: The syntax is correct. No typos found.
145
+
146
+ 7. Using HAVING with COUNT() for Additional Filtering
147
+ SELECT city, MAX(marks) AS highest_marks
148
+ FROM students
149
+ GROUP BY city
150
+ HAVING COUNT(student_id) > 5;
151
+
152
+
153
+ Explanation: This is a valid query, and it’s typo-free.
154
+
155
+ 8. Using DISTINCT to Ensure Uniqueness
156
+ SELECT DISTINCT city, name, marks
157
+ FROM students s
158
+ WHERE marks = (
159
+ SELECT MAX(marks)
160
+ FROM students
161
+ WHERE city = s.city
162
+ );
163
+
164
+
165
+ Explanation: The query looks fine, no issues here.
166
+
167
+ 9. Using SELF JOIN to Compare Students' Marks
168
+ SELECT s1.city, s1.name, s1.marks
169
+ FROM students s1
170
+ JOIN students s2
171
+ ON s1.city = s2.city
172
+ WHERE s1.marks = (SELECT MAX(marks) FROM students WHERE city = s1.city);
173
+
174
+
175
+ Explanation: This is correctly written, and the syntax is accurate.
176
+
177
+ 10. Using LIMIT (or FETCH FIRST) for Top Results
178
+ SELECT city, name, marks
179
+ FROM students s
180
+ WHERE (city, marks) IN (
181
+ SELECT city, MAX(marks)
182
+ FROM students
183
+ GROUP BY city
184
+ );
185
+
186
+
187
+ Explanation: This query is written correctly, with no typos.
188
+
189
+
190
+ Model 1: Filtering Unique Values
191
+
192
+ This is one of the most common operations — retrieving unique values from a single column.
193
+
194
+ SELECT DISTINCT column_name
195
+ FROM table_name;
196
+
197
+
198
+ Explanation: The DISTINCT keyword ensures that the query returns only unique values from the column_name.
199
+
200
+ Example:
201
+
202
+ SELECT DISTINCT city
203
+ FROM employees;
204
+
205
+
206
+ This query will return a list of unique cities from the employees table.
207
+
208
+ ------------------------------------------------------------------------------------------------------------------------------------
209
+
210
+ Model 2: Filtering with Conditions
211
+
212
+ You can filter data using conditions, for instance, only retrieving unique values that meet certain criteria.
213
+
214
+ SELECT DISTINCT column_name
215
+ FROM table_name
216
+ WHERE condition;
217
+
218
+
219
+ Example:
220
+
221
+ SELECT DISTINCT department
222
+ FROM employees
223
+ WHERE salary > 50000;
224
+
225
+
226
+ This will return unique departments where the employee's salary is greater than 50,000.
227
+
228
+ 2. Using Joins with Unique Values
229
+
230
+ When working with multiple tables, you'll often need to join them and then apply uniqueness to the results.
231
+ ------------------------------------------------------------------------------------------------------------------------------------
232
+
233
+ Model 3: Inner Join with Unique Values
234
+
235
+ Let's consider two tables: employees and departments. We want to find out unique departments where employees earn more than 50,000.
236
+
237
+ SELECT DISTINCT d.department_name
238
+ FROM employees e
239
+ INNER JOIN departments d
240
+ ON e.department_id = d.department_id
241
+ WHERE e.salary > 50000;
242
+
243
+
244
+ Explanation:
245
+
246
+ INNER JOIN combines the employees table and departments table based on the department_id.
247
+
248
+ The DISTINCT keyword ensures that the result contains unique department names.
249
+ ------------------------------------------------------------------------------------------------------------------------------------
250
+
251
+ Model 4: Left Join with Filtering
252
+
253
+ In case you want to include all employees even if they don't belong to any department (NULL for department name), you can use LEFT JOIN:
254
+
255
+ SELECT DISTINCT d.department_name
256
+ FROM employees e
257
+ LEFT JOIN departments d
258
+ ON e.department_id = d.department_id
259
+ WHERE e.salary > 50000;
260
+
261
+
262
+ Explanation: This ensures that even employees without a department are included in the results.
263
+
264
+ 3. Complex Queries Involving Aggregation and Filtering
265
+ ------------------------------------------------------------------------------------------------------------------------------------
266
+
267
+ Model 5: Filtering with Aggregation Functions
268
+
269
+ Sometimes you need to get unique results along with aggregated data (like count, average, etc.).
270
+
271
+ SELECT department_name, COUNT(DISTINCT employee_id)
272
+ FROM employees
273
+ GROUP BY department_name
274
+ HAVING COUNT(DISTINCT employee_id) > 5;
275
+
276
+
277
+ Explanation: This query finds the departments with more than 5 distinct employees.
278
+
279
+ GROUP BY: Groups the results by department name.
280
+
281
+ HAVING: Filters the results based on the aggregated value.
282
+
283
+ ------------------------------------------------------------------------------------------------------------------------------------
284
+
285
+ Model 6: Subqueries for Filtering and Uniqueness
286
+
287
+ A subquery can be useful when you need to filter based on a condition that involves uniqueness.
288
+
289
+ SELECT DISTINCT department_name
290
+ FROM employees
291
+ WHERE department_id IN (
292
+ SELECT department_id
293
+ FROM departments
294
+ WHERE location = 'New York'
295
+ );
296
+
297
+
298
+ Explanation: This finds unique department names for employees located in New York.
299
+ The subquery retrieves department IDs for New York, and the main query uses those IDs to filter employees.
300
+
301
+ 4. Mixed Queries: Combining Filters, Uniqueness, and Aggregation
302
+
303
+ ------------------------------------------------------------------------------------------------------------------------------------
304
+
305
+ Model 7: Mixed Filtering with Joins and Aggregation
306
+
307
+ Sometimes you might need to filter, aggregate, and find unique results all in one query.
308
+
309
+ SELECT e.department_id, d.department_name, COUNT(DISTINCT e.employee_id) AS total_employees
310
+ FROM employees e
311
+ INNER JOIN departments d
312
+ ON e.department_id = d.department_id
313
+ WHERE e.salary > 60000
314
+ GROUP BY e.department_id, d.department_name
315
+ HAVING COUNT(DISTINCT e.employee_id) > 5;
316
+
317
+
318
+
319
+ 5. Advanced Filtering and Unique Value Extraction
320
+ Model 8: Extracting Top Unique Values
321
+
322
+ You may want to extract the top unique values, such as finding the top 5 highest-paid employees in each department.
323
+
324
+ WITH RankedEmployees AS (
325
+ SELECT e.department_id, e.employee_id, e.salary,
326
+ RANK() OVER (PARTITION BY e.department_id ORDER BY e.salary DESC) AS rank
327
+ FROM employees e
328
+ )
329
+ SELECT department_id, employee_id, salary
330
+ FROM RankedEmployees
331
+ WHERE rank <= 5;
332
+
333
+
334
+ Explanation: This query uses a window function (RANK()) to rank employees by salary within each department and then filters the top 5.
335
+ ------------------------------------------------------------------------------------------------------------------------------------
336
+
337
+
338
+ Model 9: Using DISTINCT with Multiple Columns
339
+
340
+ You can also apply DISTINCT to multiple columns to get unique combinations of values.
341
+
342
+ SELECT DISTINCT department_name, employee_id
343
+ FROM employees e
344
+ INNER JOIN departments d
345
+ ON e.department_id = d.department_id;
346
+
347
+ ----------------------------------------------------------------------------
348
+
349
+ 1. Basic Unique Values Extraction
350
+ Problem 1: Extract Unique Cities
351
+
352
+ Given a table customers with columns customer_id, name, and city, write a query to find all the unique cities where customers reside.
353
+
354
+ SQL Query:
355
+
356
+ SELECT DISTINCT city
357
+ FROM customers;
358
+
359
+
360
+ Explanation: This will give you all the unique cities where customers are located.
361
+
362
+ ------------------------------------------------------------------------------------------------------------------------------------
363
+
364
+ Problem 2: List Unique Product Categories
365
+
366
+ In a table products with product_id, product_name, and category, find the unique categories of products.
367
+
368
+ SQL Query:
369
+
370
+ SELECT DISTINCT category
371
+ FROM products;
372
+
373
+
374
+ Explanation: This will return a list of distinct categories without duplicates.
375
+
376
+ 2. Filtering with Conditions and Uniqueness
377
+
378
+ ------------------------------------------------------------------------------------------------------------------------------------
379
+
380
+ Problem 3: Unique Employees with Salary Above a Threshold
381
+
382
+ In a table employees with employee_id, name, salary, and department, list all unique employees who earn more than 75,000.
383
+
384
+ SQL Query:
385
+
386
+ SELECT DISTINCT name
387
+ FROM employees
388
+ WHERE salary > 75000;
389
+
390
+
391
+ Explanation: This will return unique employee names who have a salary above 75,000.
392
+
393
+ ------------------------------------------------------------------------------------------------------------------------------------
394
+
395
+ Problem 4: List Unique Customers from a Specific Region
396
+
397
+ Given a customers table with customer_id, name, city, and region, extract unique customer names from the West region.
398
+
399
+ SQL Query:
400
+
401
+ SELECT DISTINCT name
402
+ FROM customers
403
+ WHERE region = 'West';
404
+
405
+
406
+ Explanation: This will return distinct customer names from the West region.
407
+
408
+ 3. Working with Joins and Uniqueness
409
+
410
+ ------------------------------------------------------------------------------------------------------------------------------------
411
+
412
+ Problem 5: Unique Departments with Employees Earning Over 50,000
413
+
414
+ Given the employees table with columns employee_id, name, salary, and department_id, and
415
+ the departments table with columns department_id and department_name, find the unique department names with employees earning over 50,000.
416
+
417
+ SQL Query:
418
+
419
+ SELECT DISTINCT d.department_name
420
+ FROM employees e
421
+ INNER JOIN departments d
422
+ ON e.department_id = d.department_id
423
+ WHERE e.salary > 50000;
424
+
425
+
426
+ Explanation: This returns distinct department names where at least one employee earns above 50,000.
427
+
428
+ ------------------------------------------------------------------------------------------------------------------------------------
429
+
430
+ Problem 6: List Unique Product Categories Sold in Specific Locations
431
+
432
+ You have two tables: sales with columns sale_id, product_id, and location, and products with columns product_id, product_name, and category.
433
+ Find all unique product categories sold in the location 'New York'.
434
+
435
+ SQL Query:
436
+
437
+ SELECT DISTINCT p.category
438
+ FROM sales s
439
+ INNER JOIN products p
440
+ ON s.product_id = p.product_id
441
+ WHERE s.location = 'New York';
442
+
443
+
444
+ Explanation: This query joins the two tables to find unique categories of products sold in 'New York'.
445
+
446
+ 4. Advanced Filtering with Aggregation
447
+
448
+ ------------------------------------------------------------------------------------------------------------------------------------
449
+
450
+ Problem 7: Unique Departments with More Than 10 Employees
451
+
452
+ Given the employees table with employee_id, department_id, and salary, find unique departments that have more than 10 employees.
453
+
454
+ SQL Query:
455
+
456
+ SELECT DISTINCT department_id
457
+ FROM employees
458
+ GROUP BY department_id
459
+ HAVING COUNT(employee_id) > 10;
460
+
461
+
462
+ Explanation: This query groups employees by department and returns those departments that have more than 10 employees.
463
+
464
+ ------------------------------------------------------------------------------------------------------------------------------------
465
+
466
+ Problem 8: List Unique Products Sold with Total Sales Over 5000
467
+
468
+ Given a sales table with sale_id, product_id, and amount, and a products table with product_id, product_name, and category, list all unique products where the total sales exceed 5000.
469
+
470
+ SQL Query:
471
+
472
+ SELECT DISTINCT p.product_name
473
+ FROM sales s
474
+ INNER JOIN products p
475
+ ON s.product_id = p.product_id
476
+ GROUP BY p.product_name
477
+ HAVING SUM(s.amount) > 5000;
478
+
479
+
480
+ Explanation: This query groups sales by product name and filters to include only products where the total sales amount exceeds 5000.
481
+
482
+ 5. Complex Queries Involving Subqueries
483
+ ------------------------------------------------------------------------------------------------------------------------------------
484
+
485
+ Problem 9: Unique Employees Who Earn More Than the Average Salary
486
+
487
+ In the employees table, with employee_id and salary, list unique employees who earn more than the average salary.
488
+
489
+ SQL Query:
490
+
491
+ SELECT DISTINCT name
492
+ FROM employees
493
+ WHERE salary > (SELECT AVG(salary) FROM employees);
494
+
495
+
496
+ Explanation: The subquery calculates the average salary, and the outer query returns distinct employees who earn more than that average.
497
+
498
+ ------------------------------------------------------------------------------------------------------------------------------------
499
+
500
+ Problem 10: List Unique Customers Who Have Ordered More Than One Product
501
+
502
+ Given a customers table with customer_id and name, and an orders table with order_id, customer_id, and product_id, find unique customers who have ordered more than one product.
503
+
504
+ SQL Query:
505
+
506
+ SELECT DISTINCT c.name
507
+ FROM customers c
508
+ INNER JOIN orders o
509
+ ON c.customer_id = o.customer_id
510
+ GROUP BY c.customer_id
511
+ HAVING COUNT(DISTINCT o.product_id) > 1;
512
+
513
+
514
+ Explanation: This query joins the customers and orders tables, groups by customer, and filters customers who ordered more than one product.
515
+
516
+ 6. Window Functions and Ranking with Uniqueness
517
+
518
+ ------------------------------------------------------------------------------------------------------------------------------------
519
+
520
+ Problem 11: Top 3 Employees by Salary in Each Department
521
+
522
+ Given the employees table with employee_id, name, department_id, and salary, find the top 3 employees by salary in each department.
523
+
524
+ SQL Query:
525
+
526
+ WITH RankedEmployees AS (
527
+ SELECT e.department_id, e.employee_id, e.name, e.salary,
528
+ RANK() OVER (PARTITION BY e.department_id ORDER BY e.salary DESC) AS rank
529
+ FROM employees e
530
+ )
531
+ SELECT department_id, employee_id, name, salary
532
+ FROM RankedEmployees
533
+ WHERE rank <= 3;
534
+
535
+
536
+ Explanation: The window function RANK() assigns a rank to each employee within their department based on salary, and the query filters the top 3 employees.
537
+
538
+ ------------------------------------------------------------------------------------------------------------------------------------
539
+
540
+ Problem 12: Unique Employees with Highest Salary in Each Department
541
+
542
+ Given the employees table, list unique employees with the highest salary in each department.
543
+
544
+ SQL Query:
545
+
546
+ WITH MaxSalary AS (
547
+ SELECT department_id, MAX(salary) AS max_salary
548
+ FROM employees
549
+ GROUP BY department_id
550
+ )
551
+ SELECT DISTINCT e.department_id, e.name, e.salary
552
+ FROM employees e
553
+ INNER JOIN MaxSalary m
554
+ ON e.department_id = m.department_id
555
+ WHERE e.salary = m.max_salary;
556
+
557
+
558
+ Explanation: The MaxSalary CTE calculates the highest salary per department, and the main query retrieves employees who earn that amount.
559
+
560
+ ------------------------------------------------------------------------------------------------------------------------------------
561
+
562
+
563
+ Topic 1: Data Filtering (Using WHERE, AND, OR, LIKE, BETWEEN, etc.)
564
+ Question 1: Basic Filtering with WHERE Clause
565
+
566
+ You have a table students with columns student_id, name, age, and score. Write a query to filter students who have a score greater than 80.
567
+
568
+ Expected Query:
569
+
570
+ SELECT name
571
+ FROM students
572
+ WHERE score > 80;
573
+
574
+
575
+ Challenge: Try filtering students whose age is between 18 and 25, and their score is greater than 75.
576
+
577
+ ------------------------------------------------------------------------------------------------------------------------------------
578
+
579
+ Question 2: Using AND and OR for Multiple Conditions
580
+
581
+ In the employees table, filter the employees who are in the 'Sales' department and have a salary greater than 50,000, or employees who have the title 'Manager'.
582
+
583
+ Expected Query:
584
+
585
+ SELECT name
586
+ FROM employees
587
+ WHERE (department = 'Sales' AND salary > 50000) OR title = 'Manager';
588
+
589
+
590
+ Challenge: Modify the query to exclude employees who have been with the company for less than 2 years (join_date column).
591
+
592
+ ------------------------------------------------------------------------------------------------------------------------------------
593
+
594
+ Question 3: Filtering with LIKE and Wildcards
595
+
596
+ In the products table, write a query to filter the product names that start with 'A'.
597
+
598
+ Expected Query:
599
+
600
+ SELECT product_name
601
+ FROM products
602
+ WHERE product_name LIKE 'A%';
603
+
604
+
605
+ Challenge: Modify the query to filter product names containing the word 'pro' anywhere in the name.
606
+
607
+ ------------------------------------------------------------------------------------------------------------------------------------
608
+
609
+ Question 4: Filtering with BETWEEN
610
+
611
+ You are given a sales table with sale_id, amount, and date. Write a query to filter sales that happened between '2025-01-01' and '2025-06-30'.
612
+
613
+ Expected Query:
614
+
615
+ SELECT sale_id
616
+ FROM sales
617
+ WHERE date BETWEEN '2025-01-01' AND '2025-06-30';
618
+
619
+
620
+ Challenge: Now, filter sales with amounts between 1000 and 5000.
621
+ ------------------------------------------------------------------------------------------------------------------------------------
622
+
623
+ Question 5: Filtering with IN
624
+
625
+ You are working with a students table. Filter students who belong to either the 'Math', 'Science', or 'English' departments.
626
+
627
+ Expected Query:
628
+
629
+ SELECT name
630
+ FROM students
631
+ WHERE department IN ('Math', 'Science', 'English');
632
+
633
+
634
+ Challenge: Modify the query to include only those students whose score is greater than or equal to 85.
635
+
636
+ ------------------------------------------------------------------------------------------------------------------------------------
637
+
638
+ Topic 2: Unique Values (Using DISTINCT, Aggregations, and GROUP BY)
639
+ Question 6: Extracting Unique Values Using DISTINCT
640
+
641
+ Given a customers table with customer_id and city, write a query to find all unique cities where customers reside.
642
+
643
+ Expected Query:
644
+
645
+ SELECT DISTINCT city
646
+ FROM customers;
647
+
648
+
649
+ Challenge: Modify the query to list unique cities where the number of customers is greater than 50.
650
+
651
+ ------------------------------------------------------------------------------------------------------------------------------------
652
+
653
+ Question 7: Filtering Unique Values with Aggregations
654
+
655
+ In the employees table, list all unique departments where the average salary is greater than 60,000.
656
+
657
+ Expected Query:
658
+
659
+ SELECT DISTINCT department
660
+ FROM employees
661
+ GROUP BY department
662
+ HAVING AVG(salary) > 60000;
663
+
664
+
665
+ Challenge: Add a filter to include only departments with more than 10 employees.
666
+
667
+ ------------------------------------------------------------------------------------------------------------------------------------
668
+
669
+ Question 8: Using DISTINCT with Multiple Columns
670
+
671
+ You have a table orders with order_id, customer_id, and product_id. Write a query to return unique combinations of customer and product.
672
+
673
+ Expected Query:
674
+
675
+ SELECT DISTINCT customer_id, product_id
676
+ FROM orders;
677
+
678
+
679
+ Challenge: Modify the query to list unique customer and product combinations where the product_id is greater than 100.
680
+
681
+ ------------------------------------------------------------------------------------------------------------------------------------
682
+
683
+ Question 9: Combining DISTINCT with WHERE
684
+
685
+ In the employees table with employee_id, name, salary, and department, find unique departments where employees earn more than 50,000.
686
+
687
+ Expected Query:
688
+
689
+ SELECT DISTINCT department
690
+ FROM employees
691
+ WHERE salary > 50000;
692
+
693
+
694
+ Challenge: Add a condition to exclude departments where the salary is greater than 100,000.
695
+
696
+ ------------------------------------------------------------------------------------------------------------------------------------
697
+
698
+ Question 10: Grouping and Filtering Unique Values
699
+
700
+ In a sales table with sale_id, product_id, amount, and date, find the unique products sold in 2025 where the total sales amount is greater than 10,000.
701
+
702
+ Expected Query:
703
+
704
+ SELECT DISTINCT product_id
705
+ FROM sales
706
+ WHERE YEAR(date) = 2025
707
+ GROUP BY product_id
708
+ HAVING SUM(amount) > 10000;
709
+
710
+
711
+ Challenge: Add a filter to exclude products sold in the first quarter of 2025 (January, February, March).
712
+
713
+ Bonus Challenge: Complex Query Involving Joins and Uniqueness
714
+
715
+ ------------------------------------------------------------------------------------------------------------------------------------
716
+
717
+ Question 11: Join and Extract Unique Values
718
+
719
+ You are given two tables: employees with employee_id, name, and department_id, and departments with department_id and department_name. Write a query to find unique department names where the average salary of employees is greater than 50,000.
720
+
721
+ Expected Query:
722
+
723
+ SELECT DISTINCT d.department_name
724
+ FROM employees e
725
+ INNER JOIN departments d
726
+ ON e.department_id = d.department_id
727
+ GROUP BY d.department_name
728
+ HAVING AVG(e.salary) > 50000;