11``` sql
22-- Create a table named department
33CREATE TABLE department (
4- dept_name VARCHAR (100 ),
4+ dept_name VARCHAR (100 ) PRIMARY KEY ,
55 building VARCHAR (120 ) NOT NULL ,
6- budget NUMERIC (12 ,2 ) NOT NULL DEFAULT ' undecided' ,
7- num_staff INT ,
8- PRIMARY KEY (dept_name)
6+ budget NUMERIC (12 ,2 ) NOT NULL DEFAULT 0 .00 ,
7+ num_staff INT
98);
109
1110-- Insert values into the department table
11+ -- Removed duplicates because dept_name is a PRIMARY KEY
1212INSERT INTO department (dept_name, building, budget, num_staff)
13- VALUES (' cse' , ' ma wazed' , 12 .20 , 10 ),
14- (' cse' , ' ma wazed' , 12 .20 , 10 ),
15- (' cse' , ' ma wazed' , 12 .20 , 10 );
13+ VALUES (' cse' , ' ma wazed' , 12 .20 , 10 );
1614
1715-- Add a column named dept_code to the existing department table
18- ALTER TABLE department ADD dept_code VARCHAR (6 ) NOT NULL ;
16+ ALTER TABLE department ADD COLUMN dept_code VARCHAR (6 ) NOT NULL DEFAULT ' NA ' ;
1917
20- -- Add a column named dep_code to the existing department table after the dept_name column
21- ALTER TABLE department ADD dep_code VARCHAR (6 ) NOT NULL AFTER dept_name;
18+ -- Add a column named dep_code to the existing department table
19+ -- PostgreSQL does not support 'AFTER', so we just add it
20+ ALTER TABLE department ADD COLUMN dep_code VARCHAR (6 ) NOT NULL DEFAULT ' NA' ;
2221
2322-- Remove the dept_code column from the department table
2423ALTER TABLE department DROP COLUMN dept_code;
2524
26- -- Modify the data type of the dept_code column to VARCHAR(10) in the department table
27- ALTER TABLE department MODIFY COLUMN dept_code VARCHAR (10 );
28-
29- -- Delete the department table along with its structure
30- DROP TABLE department;
25+ -- Modify the data type of the dep_code column to VARCHAR(10) in the department table
26+ ALTER TABLE department ALTER COLUMN dep_code TYPE VARCHAR (10 );
3127
3228-- Delete all data from the department table, keeping the table structure
3329DELETE FROM department;
3430-- OR
3531TRUNCATE department;
3632
33+ -- Delete the department table along with its structure
34+ DROP TABLE department;
35+
3736-- Insert values into a table (TABLE2) from another table (TABLE1) based on a condition
37+ -- Replace CONDITION with an actual condition
3838INSERT INTO TABLE2 (employeeId, employeeName)
39- SELECT id, firstName FROM TABLE1 WHERE CONDITION ;
39+ SELECT id, firstName FROM TABLE1 WHERE 1 = 1 ;
4040
4141-- Update the salary column of the instructor table by increasing it by 5% for instructors with salary below the average salary
4242UPDATE instructor
@@ -63,7 +63,7 @@ LIMIT n;
6363-- Retrieve a limited number of rows from a table, starting from the mth row
6464SELECT *
6565FROM TABLE_NAME
66- LIMIT m, n ;
66+ LIMIT n OFFSET m ;
6767
6868-- Retrieve records from the instructor table where the name is not 'pranto' or 'Zahid'
6969SELECT *
@@ -113,10 +113,11 @@ SELECT dept_name, COUNT(id) AS num_of_teacher
113113FROM instructor
114114GROUP BY dept_name;
115115
116- -- Perform a natural join between the employee and branch tables
116+ -- Perform a join between the employee and branch tables
117+ -- Replaced [LEFT|RIGHT] with LEFT JOIN
117118SELECT branch_name, first_name, last_name
118119FROM employee
119- [ LEFT|RIGHT] JOIN branch
120+ LEFT JOIN branch
120121ON employee .emp_id = branch .mgr_id ;
121122
122123-- Perform a nested query to retrieve employee names based on their IDs in the works_with table
@@ -129,33 +130,38 @@ WHERE employee.emp_id IN (
129130);
130131
131132-- Create a trigger that inserts values into another table when a new row is inserted into table1
132- DELIMITER |
133- CREATE OR REPLACE TRIGGER triggertest
134- BEFORE INSERT
135- ON table1
136- FOR EACH ROW
133+ CREATE OR REPLACE FUNCTION triggertest_func ()
134+ RETURNS TRIGGER AS $$
137135BEGIN
138136 INSERT INTO table2 VALUES (NEW .id , NEW .name );
137+ RETURN NEW;
139138END;
140- |
141- DELIMITER ;
139+ $$ LANGUAGE plpgsql;
142140
143- -- Create a trigger with conditional statements that inserts values into another table based on the sex column in the employee table
144- DELIMITER |
145- CREATE TRIGGER my_trigger BEFORE INSERT
146- ON employee
141+ CREATE TRIGGER my_trigger1
142+ BEFORE INSERT ON table1
147143FOR EACH ROW
144+ EXECUTE FUNCTION triggertest_func();
145+
146+ -- Create a trigger with conditional statements that inserts values into another table based on the sex column in the employee table
147+ CREATE OR REPLACE FUNCTION my_trigger_func ()
148+ RETURNS TRIGGER AS $$
148149BEGIN
149150 IF NEW .sex = ' M' THEN
150151 INSERT INTO trigger_test VALUES (' added male employee' );
151- ELSEIF NEW .sex = ' F' THEN
152+ ELSIF NEW .sex = ' F' THEN
152153 INSERT INTO trigger_test VALUES (' added female' );
153154 ELSE
154155 INSERT INTO trigger_test VALUES (' added other employee' );
155156 END IF;
157+ RETURN NEW;
156158END;
157- |
158- DELIMITER ;
159+ $$ LANGUAGE plpgsql;
160+
161+ CREATE TRIGGER my_trigger2
162+ BEFORE INSERT ON employee
163+ FOR EACH ROW
164+ EXECUTE FUNCTION my_trigger_func();
159165
160166-- Example of a complex query involving multiple tables and joins
161167SELECT t .eName
@@ -168,5 +174,3 @@ INNER JOIN employee AS e
168174ON t .mName = e .eName
169175AND t .city = e .city
170176AND t .street = e .street ;
171-
172- ```
0 commit comments