-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql-fundamentals-practice.sql
More file actions
153 lines (133 loc) · 3.29 KB
/
Copy pathsql-fundamentals-practice.sql
File metadata and controls
153 lines (133 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
-- Drop the table if it already exists
DROP TABLE IF EXISTS employees;
-- Create the employees table
CREATE TABLE employees (
employee_id SERIAL PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
department VARCHAR(50),
salary DECIMAL(10, 2) CHECK (salary > 0),
joining_date DATE NOT NULL,
age INT CHECK (age >= 18)
);
-- Insert data into employees table
INSERT INTO employees (first_name, last_name, department, salary, joining_date, age)
VALUES ('Mohan', 'Thurpati', 'IT', 60000.00, '2022-05-01', 29),
('Rama', 'Patel', 'HR', 55000.00, '2021-08-15', 32),
('Ravi', 'Kumar', 'Finance', 70000.00, '2020-03-10', 35),
('Shiva', 'Varma', 'IT', 65000.00, '2019-11-22', 28),
('Rashekar', 'Reddy', 'Operations', 50000.00, '2023-01-10', 26);
--To See Table Data
SELECT * FROM employees;
-- Assignment Questions
--Q1: Retrieve all employees’ first_name and their departments.
SELECT
first_name,
department
FROM
employees;
--Q2: Update the salary of all employees in the 'IT' department by increasing it by 10%.
UPDATE employees
SET
salary = salary * 1.10
WHERE
department = 'IT';
SELECT
*
FROM
employees;
--Q3: Delete all employees who are older than 34 years.
DELETE FROM employees
WHERE
age > 34;
--To Verify update
SELECT * FROM employees;
--Q4: Add a new column `email` to the `employees` table.
ALTER TABLE employees
ADD COLUMN email VARCHAR(100);
--Q5: Rename the `department` column to `dept_name`.
ALTER TABLE employees
RENAME COLUMN department TO dept_name;
SELECT
*
FROM
employees;
--Q6: Retrieve the names of employees who joined after January 1, 2021.
SELECT
first_name,
last_name,
joining_date
FROM
employees
WHERE
joining_date > '2021-01-01';
--Q7: Change the data type of the `salary` column to `INTEGER`.
ALTER TABLE employees
ALTER COLUMN salary TYPE INTEGER;
--Q8: List all employees with their age and salary in descending order of salary.
SELECT
first_name,
last_name,
age,
salary
FROM
employees
ORDER BY
salary DESC;
--Q9: Insert a new employee with the following details:
-- ('Raj', 'Singh', 'Marketing', 60000, '2023-09-15', 30)
INSERT INTO employees (first_name, last_name, dept_name, salary, joining_date, age)
VALUES ('Raj', 'Singh', 'Marketing', 60000, '2023-09-15', 30);
--Q10: Update age of employee +1 to every employee
UPDATE employees
SET
age = age + 1;
----To Verify update
SELECT * FROM employees;
--Q11. Find the total salary paid to each department.
SELECT
dept_name,
SUM(salary) AS total_salary
FROM
employees
GROUP BY
dept_name;
--Q12. Find the average salary of employees in each department.
SELECT
dept_name,
ROUND(AVG(salary),2) AS avg_salary
FROM
employees
GROUP BY
dept_name;
--Q13. Display the top 3 highest-paid employees.
SELECT
employee_id,
first_name,
last_name,
salary
FROM
employees
ORDER BY
salary DESC
LIMIT 3;
--Q14. Find departments that have more than one employee.
SELECT
dept_name,
COUNT(*) AS dept_count
FROM
employees
GROUP BY
dept_name
HAVING
COUNT(*) > 1;
--Q15. Find employees who joined after January 1, 2021 and earn more than ₹55,000.
SELECT
first_name,
last_name,
salary,
joining_date
FROM
employees
WHERE
joining_date > '2021-01-01' and salary>55000;