Skip to content

Commit f3909f6

Browse files
Fixed bugs in psql-query.md file
1 parent 2137da0 commit f3909f6

1 file changed

Lines changed: 55 additions & 75 deletions

File tree

psql-query.md

Lines changed: 55 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,110 @@
11
```sql
2-
-- Create a table named department
2+
-- 1. Create a table named department
33
CREATE TABLE department (
44
dept_name VARCHAR(100) PRIMARY KEY,
55
building VARCHAR(120) NOT NULL,
6-
budget NUMERIC(12,2) NOT NULL DEFAULT 'undecided',
6+
budget NUMERIC(12,2) NOT NULL DEFAULT 0.00,
77
num_staff INT
88
);
99

10-
-- Insert values into the department table
10+
-- 2. Insert unique values into the department table
1111
INSERT INTO department (dept_name, building, budget, num_staff)
12-
VALUES ('cse', 'ma wazed', 12.20, 10),
13-
('cse', 'ma wazed', 12.20, 10),
14-
('cse', 'ma wazed', 12.20, 10);
12+
VALUES ('CSE', 'MA Wazed', 12000.00, 10),
13+
('EEE', 'Main Building', 15000.00, 8),
14+
('ME', 'Workshop Block', 13000.00, 12);
1515

16-
-- Add a column named dept_code to the existing department table
17-
ALTER TABLE department ADD COLUMN dept_code VARCHAR(6) NOT NULL;
16+
-- 3. Add new columns with default values
17+
ALTER TABLE department ADD COLUMN dep_code VARCHAR(15) DEFAULT 'NA';
1818

19-
-- Add a column named dep_code to the existing department table after the dept_name column
20-
ALTER TABLE department ADD COLUMN dep_code VARCHAR(6) NOT NULL AFTER dept_name;
21-
22-
-- Remove the dept_code column from the department table
23-
ALTER TABLE department DROP COLUMN dept_code;
24-
25-
-- Modify the data type of the dept_code column to VARCHAR(10) in the department table
26-
ALTER TABLE department ALTER COLUMN dept_code TYPE VARCHAR(10);
27-
28-
-- Delete the department table along with its structure
29-
DROP TABLE department;
30-
31-
-- Delete all data from the department table, keeping the table structure
32-
DELETE FROM department;
33-
-- OR
34-
TRUNCATE department;
35-
36-
-- Insert values into a table (TABLE2) from another table (TABLE1) based on a condition
37-
INSERT INTO TABLE2 (employeeId, employeeName)
38-
SELECT id, firstName FROM TABLE1 WHERE CONDITION;
19+
-- 4. Insert values into another table from a table
20+
-- Make sure employee_archive exists
21+
INSERT INTO employee_archive (employeeId, employeeName)
22+
SELECT emp_id, first_name
23+
FROM employee
24+
WHERE salary > 50000;
3925

40-
-- Update the salary column of the instructor table by increasing it by 5% for instructors with salary below the average salary
26+
-- 5. Update instructor salary below average by 5%
4127
UPDATE instructor
4228
SET salary = salary * 1.05
4329
WHERE salary < (SELECT AVG(salary) FROM instructor);
4430

45-
-- Update the salary column of the instructor table using a case statement
31+
-- 6. Update salary using CASE
4632
UPDATE instructor
4733
SET salary = CASE
4834
WHEN salary <= 100000 THEN salary * 1.05
4935
ELSE salary * 1.03
5036
END;
5137

52-
-- Retrieve records from the instructor table and order the results by dept_name in descending order and salary in ascending order
38+
-- 7. Select instructors ordered by dept_name DESC, salary ASC
5339
SELECT *
5440
FROM instructor
5541
ORDER BY dept_name DESC, salary ASC;
5642

57-
-- Retrieve a limited number of rows from a table
43+
-- 8. Retrieve limited rows
5844
SELECT *
59-
FROM TABLE_NAME
60-
LIMIT n;
45+
FROM instructor
46+
LIMIT 5; -- first 5 rows
6147

62-
-- Retrieve a limited number of rows from a table, starting from the mth row
48+
-- 9. Retrieve rows with offset
6349
SELECT *
64-
FROM TABLE_NAME
65-
LIMIT n OFFSET m;
50+
FROM instructor
51+
LIMIT 5 OFFSET 10; -- rows 11-15
6652

67-
-- Retrieve records from the instructor table where the name is not 'pranto' or 'Zahid'
53+
-- 10. Select where name not in list
6854
SELECT *
6955
FROM instructor
70-
WHERE name NOT IN ('pranto', 'Zahid');
56+
WHERE name NOT IN ('Pranto', 'Zahid');
7157

72-
-- Retrieve records from the instructor table where the id is within a range
58+
-- 11. Select where id is within a subquery
7359
SELECT *
7460
FROM instructor
7561
WHERE id IN (SELECT id FROM instructor WHERE id < 500);
7662

77-
-- Retrieve records from the instructor table where the salary is between 5000 and 10000
63+
-- 12. Select salary between 5000 and 10000
7864
SELECT sname
7965
FROM instructor
8066
WHERE salary BETWEEN 5000 AND 10000;
8167

82-
-- Retrieve records from the instructor table where the salary is NULL
68+
-- 13. Select rows where salary is NULL
8369
SELECT *
8470
FROM instructor
8571
WHERE salary IS NULL;
8672

87-
-- Retrieve records from the instructor table where the id ends with '5' followed by any character
73+
-- 14. Select id ending with '5' and any character
8874
SELECT *
8975
FROM instructor
90-
WHERE id LIKE '%5_';
76+
WHERE id::TEXT LIKE '%5_';
9177

92-
-- Find the average salary of instructors in the CSE department
93-
SELECT AVG(salary)
78+
-- 15. Average salary of CSE department
79+
SELECT AVG(salary) AS avg_salary
9480
FROM instructor
9581
WHERE dept_name = 'CSE';
9682

97-
-- Find the number of departments in the instructor relation
98-
SELECT COUNT(DISTINCT dept_name)
83+
-- 16. Number of distinct departments
84+
SELECT COUNT(DISTINCT dept_name) AS num_departments
9985
FROM instructor;
10086

101-
-- Find the number of tuples (rows) in the instructor relation
87+
-- 17. Number of rows in instructor table
10288
SELECT COUNT(*) AS num_rows
10389
FROM instructor;
10490

105-
-- Retrieve the department name and the average salary of instructors for each department
91+
-- 18. Department name and average salary
10692
SELECT dept_name, AVG(salary) AS avg_salary
10793
FROM instructor
10894
GROUP BY dept_name;
10995

110-
-- Retrieve the department name and the number of instructors in each department
96+
-- 19. Department name and number of instructors
11197
SELECT dept_name, COUNT(id) AS num_of_teacher
11298
FROM instructor
11399
GROUP BY dept_name;
114100

115-
-- Perform a natural join between the employee and branch tables
116-
SELECT branch_name, first_name, last_name
101+
-- 20. Join example: LEFT JOIN employee and branch
102+
SELECT branch.branch_name, employee.first_name, employee.last_name
117103
FROM employee
118-
[LEFT|RIGHT] JOIN branch
104+
LEFT JOIN branch
119105
ON employee.emp_id = branch.mgr_id;
120106

121-
-- Perform a nested query to retrieve employee names based on their IDs in the works_with table
107+
-- 21. Nested query example
122108
SELECT employee.first_name, employee.last_name
123109
FROM employee
124110
WHERE employee.emp_id IN (
@@ -127,7 +113,7 @@ WHERE employee.emp_id IN (
127113
WHERE total_sales > 30000
128114
);
129115

130-
-- Create a trigger that inserts values into another table when a new row is inserted into table1
116+
-- 22. Trigger example: insert into table2 when a row is inserted in table1
131117
CREATE OR REPLACE FUNCTION triggertest()
132118
RETURNS TRIGGER AS $$
133119
BEGIN
@@ -141,36 +127,30 @@ BEFORE INSERT ON table1
141127
FOR EACH ROW
142128
EXECUTE FUNCTION triggertest();
143129

144-
-- Create a trigger with conditional statements that inserts values into another table based on the sex column in the employee table
145-
CREATE OR REPLACE FUNCTION my_trigger()
130+
-- 23. Trigger with conditional logic
131+
CREATE OR REPLACE FUNCTION sex_trigger()
146132
RETURNS TRIGGER AS $$
147133
BEGIN
148134
IF NEW.sex = 'M' THEN
149135
INSERT INTO trigger_test VALUES ('added male employee');
150136
ELSIF NEW.sex = 'F' THEN
151-
INSERT INTO trigger_test VALUES ('added female');
137+
INSERT INTO trigger_test VALUES ('added female employee');
152138
ELSE
153139
INSERT INTO trigger_test VALUES ('added other employee');
154140
END IF;
155141
RETURN NEW;
156142
END;
157143
$$ LANGUAGE plpgsql;
158144

159-
CREATE TRIGGER my_trigger
145+
CREATE TRIGGER my_sex_trigger
160146
BEFORE INSERT ON employee
161147
FOR EACH ROW
162-
EXECUTE FUNCTION my_trigger();
163-
164-
-- Example of a complex query involving multiple tables and joins
165-
SELECT t.eName
166-
FROM (
167-
SELECT *
168-
FROM employee
169-
NATURAL JOIN manages
170-
) AS t
171-
INNER JOIN employee AS e
172-
ON t.mName = e.eName
173-
AND t.city = e.city
174-
AND t.street = e.street;
175-
176-
```
148+
EXECUTE FUNCTION sex_trigger();
149+
150+
-- 24. Complex query: explicit JOIN instead of NATURAL JOIN
151+
SELECT e1.eName
152+
FROM manages m
153+
INNER JOIN employee e1 ON m.emp_id = e1.emp_id
154+
INNER JOIN employee e2 ON m.mName = e2.eName
155+
AND m.city = e2.city
156+
AND m.street = e2.street;

0 commit comments

Comments
 (0)