Here’s the complete explanation with SQL queries, outputs, and an extra "Explain" column to make it clearer. 1️⃣ Using BETWEEN (Inclusive) SELECT * FROM employees WHERE experience_years BETWEEN 3 AND 7; ✅ Includes employees with 3, 4, 5, 6, and 7 years of experience. Output Example: employee_id name experience_years Explain 101 Alice 3 Included (3 is in range) 102 Bob 4 Included (between 3 and 7) 103 Carol 5 Included (between 3 and 7) 104 David 6 Included (between 3 and 7) 105 Eve 7 Included (7 is in range) 2️⃣ Using >= and <= (Inclusive, Explicit Condition) SELECT * FROM employees WHERE experience_years >= 3 AND experience_years <= 7; ✅ Same result as BETWEEN , but more flexible for modifications. Output Example: employee_id name experience_years Explain 101 Alice 3 Included (3 is in range) 102 Bob 4 Included (between 3 and 7) 103 Carol 5 Included (between 3 and 7) 104 Da...
Posts
Showing posts from April, 2025
ORDER BY, GROUP BY, HAVING, and WHERE clauses
- Get link
- X
- Other Apps
Sure! Let's start by creating a sample table and then go through ORDER BY , GROUP BY , HAVING , and WHERE clauses in detail with 10 examples . Step 1: Create a Sample Table We will create an employees table to work with. CREATE TABLE employees ( id INT PRIMARY KEY, name VARCHAR(50), department VARCHAR(50), salary INT, experience INT ); Step 2: Insert Sample Data INSERT INTO employees (id, name, department, salary, experience) VALUES (1, 'Alice', 'HR', 60000, 5), (2, 'Bob', 'IT', 75000, 3), (3, 'Charlie', 'IT', 65000, 7), (4, 'David', 'Finance', 50000, 2), (5, 'Eve', 'Finance', 70000, 6), (6, 'Frank', 'IT', 80000, 8), (7, 'Grace', 'HR', 45000, 4), (8, 'Hank', 'IT', 90000, 9), (9, 'Ivy', 'Finance', 60000, 3), (10, 'Jack', 'HR', 55000, 6); Step 3: Understanding Clauses 1. WHERE Clause Filters...