Write a query to fetch all employees whose salary is greater than 50,000.
Task:
Write a SQL query to fetch all employees whose salary is greater than 50,000.
Step-by-Step Detailed Solution
Step 1: Create the Table (if not already created)
We will create the employees
table:
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(100),
department VARCHAR(50),
salary DECIMAL(10,2),
hire_date DATE
);
Step 2: Insert Dummy Data
We insert some sample records:
INSERT INTO employees (id, name, department, salary, hire_date) VALUES
(1, 'John Smith', 'HR', 50000.00, '2020-01-15'),
(2, 'Alice Johnson', 'Engineering', 75000.00, '2019-03-22'),
(3, 'Bob Lee', 'Sales', 62000.00, '2021-07-10'),
(4, 'Mary Jane', 'Engineering', 80000.00, '2018-11-05'),
(5, 'Tom Hardy', 'Marketing', 55000.00, '2022-06-01'),
(6, 'Linda Park', 'Support', 48000.00, '2023-01-20');
Step 3: Write the Query
Now, we write a query to fetch employees whose salary is greater than 50,000:
SELECT * FROM employees
WHERE salary > 50000;
Step 4: Output (Result)
After executing the above query, you'll get the following result:
id |
name |
department |
salary |
hire_date |
2 |
Alice Johnson |
Engineering |
75000.00 |
2019-03-22 |
3 |
Bob Lee |
Sales |
62000.00 |
2021-07-10 |
4 |
Mary Jane |
Engineering |
80000.00 |
2018-11-05 |
5 |
Tom Hardy |
Marketing |
55000.00 |
2022-06-01 |
Explanation:
- The query uses the
WHERE
clause to filter rows where salary > 50000
.
- The
SELECT *
returns all columns from those filtered rows.
If you'd like to sort the result by salary or name, or only show name and salary, here are a few options:
Sort by Salary (Descending)
SELECT * FROM employees
WHERE salary > 50000
ORDER BY salary DESC;
Show only name and salary
SELECT name, salary FROM employees
WHERE salary > 50000;