Structure Query Language
Theory Coming Soon
Theory Coming Soon
You have table EMPLOYEES (ID, Name, DeptID) and table DEPARTMENTS (DeptID, DeptName). Write the logical steps (or pseudo-SQL logic) to get a list of Employee Names who work in "IT"..CB, O(it-22), 2026

Given Tables:
EMPLOYEES (ID, Name, DeptID)
DEPARTMENTS (DeptID, DeptName)

Objective:
To get the list of employee names who work in the IT department.

Logical Steps:

  • Identify the DeptID of the department where DeptName is IT.
  • Match this DeptID with the DeptID in the EMPLOYEES table.
  • Select the Name of employees who belong to the IT department.

Pseudo-SQL Logic:

SELECT E.Name
FROM EMPLOYEES E
JOIN DEPARTMENTS D
  ON E.DeptID = D.DeptID
WHERE D.DeptName = 'IT';
Consider a STUDENTS table with the following attributes: StudentID, Name, and Department,
(i) Write an SQL query to display only StudentID, Name, and Marks for students scoring more than 80 marks.
(ii) Write an SQL query to count how many students scored more than 80 marks in each Department.CB, O(it-23), 2026

(i) SQL Query to Display StudentID, Name, and Marks for Students Scoring More Than 80 Marks

SELECT StudentID, Name, Marks
FROM STUDENTS
WHERE Marks > 80;

Explanation:

  • SELECT chooses the required columns.
  • FROM STUDENTS specifies the table name.
  • WHERE Marks > 80 filters students who scored more than 80 marks.

(ii) SQL Query to Count Students Scoring More Than 80 Marks in Each Department

SELECT Department, COUNT(*) AS Total_Students
FROM STUDENTS
WHERE Marks > 80
GROUP BY Department;

Explanation:

  • COUNT(*) counts the number of students.
  • GROUP BY Department groups records department-wise.
  • WHERE Marks > 80 considers only students scoring above 80.

(i) 80-এর বেশি Marks পাওয়া Student-দের StudentID, Name এবং Marks প্রদর্শনের SQL Query

SELECT StudentID, Name, Marks
FROM STUDENTS
WHERE Marks > 80;

ব্যাখ্যা:

  • SELECT প্রয়োজনীয় column নির্বাচন করে।
  • FROM STUDENTS table-এর নাম নির্দেশ করে।
  • WHERE Marks > 80 80-এর বেশি marks পাওয়া student filter করে।

(ii) প্রতিটি Department-এ 80-এর বেশি Marks পাওয়া Student সংখ্যা গণনার SQL Query

SELECT Department, COUNT(*) AS Total_Students
FROM STUDENTS
WHERE Marks > 80
GROUP BY Department;

ব্যাখ্যা:

  • COUNT(*) student সংখ্যা গণনা করে।
  • GROUP BY Department department অনুযায়ী group তৈরি করে।
  • WHERE Marks > 80 শুধুমাত্র 80-এর বেশি marks পাওয়া student বিবেচনা করে।
A database has a Managers table and an Employees table. The Employees table references the Managers table using a Foreign Key. The database is set to ON DELETE CASCADE
(a) Explain what ON DELETE CASCADE means in simple English.
(b) If you delete one Manager from the database, what will automatically happen to all Employees under that manager?
(c) Explain why this is very dangerous in a real banking systers. What is the best practice and how does it fix this danger?CB, SO(it-23), 2026

(a) Meaning of ON DELETE CASCADE
ON DELETE CASCADE means that if a record in the parent table is deleted, all related records in the child table will also be automatically deleted.
It maintains referential integrity automatically.

(b) Effect of deleting a Manager
If a Manager is deleted from the Managers table, then all Employees linked to that Manager via Foreign Key will also be automatically deleted from the Employees table.

(c) Why it is dangerous in banking system & best practice

Danger:
In a banking system, deleting one Manager accidentally could delete all related employee records or dependent data.
This can cause massive data loss, financial inconsistency, and system failure.

Example risk:
One wrong delete operation → many employee records permanently removed.

Best Practice:
Instead of ON DELETE CASCADE, use:

  • ON DELETE RESTRICT or NO ACTION
  • Soft delete (use a status field like “inactive” instead of deleting data)

How it fixes the problem:
– Prevents accidental mass deletion
– Keeps historical data safe
– Allows controlled data management and auditing

Database Concept: ON DELETE CASCADE

(a) ON DELETE CASCADE এর অর্থ
ON DELETE CASCADE মানে হলো parent table-এর কোনো record delete করলে তার সাথে সম্পর্কিত child table-এর সব record স্বয়ংক্রিয়ভাবে delete হয়ে যায়।
এটি referential integrity বজায় রাখে।

(b) Manager delete করলে কী হবে
যদি Managers table থেকে কোনো Manager delete করা হয়, তাহলে Foreign Key দিয়ে যুক্ত Employees table-এর সেই Manager-এর সব employee record স্বয়ংক্রিয়ভাবে delete হয়ে যাবে।

(c) Banking system-এ কেন বিপজ্জনক & best practice

বিপদ:
Banking system-এ ভুল করে একজন Manager delete করলে তার সাথে যুক্ত সব employee data delete হয়ে যেতে পারে।
এতে massive data loss, financial inconsistency এবং system failure হতে পারে।

Example risk:
এক ভুল delete operation → অনেক employee record permanently delete।

Best Practice:
ON DELETE CASCADE এর পরিবর্তে ব্যবহার করা উচিত:

  • ON DELETE RESTRICT বা NO ACTION
  • Soft delete (status field যেমন “inactive” ব্যবহার করা)

কিভাবে এটি সমস্যা সমাধান করে:
– ভুল করে mass deletion আটকায়
– পুরনো data নিরাপদ রাখে
– controlled data management এবং auditing সম্ভব করে

Explain the distinct filtering behaviors of a WHERE clause versus a HAVING clause withing a SQL statement containing an aggregate execution layout.RAKUB, ANSE, 2026
WHERE vs HAVING Clause in SQLIn SQL, both WHERE and HAVING are used to filter data, but they operate at different stages of query execution and serve different purposes. Understanding their distinct behaviors is essential when working with aggregate functions like SUM, COUNT, AVG, MIN, and MAX.1. WHERE ClauseThe WHERE clause filters individual rows before any grouping or aggregation takes place. It operates on raw data from the table.
  • Stage: Executes before GROUP BY and aggregate functions.
  • Scope: Filters individual rows based on column values.
  • Aggregate functions: Cannot use aggregate functions like COUNT(), SUM(), or AVG().
  • Performance: Reduces the number of rows early, making aggregation faster.
Example: Select only employees from the Sales department before calculating average salary.
SELECT dept, AVG(salary) FROM employees WHERE dept = 'Sales' GROUP BY dept;
Here, WHERE filters rows where dept is ‘Sales’ first, then AVG is calculated only on those rows.2. HAVING ClauseThe HAVING clause filters groups of rows after grouping and aggregation have been completed. It operates on the result of aggregate functions.
  • Stage: Executes after GROUP BY and aggregate functions.
  • Scope: Filters groups based on aggregate results.
  • Aggregate functions: Can and typically does use aggregate functions.
  • Performance: Works on already aggregated data, so it cannot reduce early row processing.
Example: Show only departments where the average salary is greater than 50,000.
SELECT dept, AVG(salary) FROM employees GROUP BY dept HAVING AVG(salary) > 50000;
Here, GROUP BY creates department groups, AVG calculates the average, and HAVING filters out groups where the average is too low.3. Execution Order in SQLSQL processes clauses in this order, not in the order they appear in the query:
OrderClauseAction
1FROMIdentify the source table
2WHEREFilter individual rows
3GROUP BYGroup the filtered rows
4AggregateApply SUM, COUNT, AVG, etc.
5HAVINGFilter grouped results
6SELECTChoose columns to display
7ORDER BYSort the final output
4. Combined ExampleFind departments in the Sales region where total sales exceed 100,000.SELECT region, SUM(sales) AS total FROM orders WHERE region = 'Sales' GROUP BY region HAVING SUM(sales) > 100000;
  • WHERE region = ‘Sales’: Filters individual rows to only Sales region orders.
  • GROUP BY region: Groups the remaining rows by region.
  • SUM(sales): Calculates total sales per region.
  • HAVING SUM(sales) > 100000: Keeps only regions where the total exceeds 100,000.
SQL-এ WHERE vs HAVING ClauseSQL-এ WHERE এবং HAVING উভয়ই data filter করতে ব্যবহৃত হয়, কিন্তু তারা query execution-এর ভিন্ন stages-এ operate করে এবং ভিন্ন purposes serve করে। Aggregate functions যেমন SUM, COUNT, AVG, MIN এবং MAX-এর সাথে কাজ করার সময় তাদের distinct behaviors বোঝা essential।1. WHERE ClauseWHERE clause individual rows filter করে কোনো grouping বা aggregation হওয়ার আগেই। এটি table থেকে raw data-এর উপর operate করে।<
  • Stage: GROUP BY এবং aggregate functions-এর আগে execute করে।
  • Scope: Column values-এর উপর ভিত্তি করে individual rows filter করে।
  • Aggregate functions: COUNT(), SUM(), AVG() এর মতো aggregate functions ব্যবহার করা যায় না।
  • Performance: Early rows কমায়, aggregation faster করে।
Example: Average salary calculate করার আগে শুধু Sales department-এর employees select করা।SELECT dept, AVG(salary) FROM employees WHERE dept = 'Sales' GROUP BY dept;এখানে WHERE প্রথমে dept ‘Sales’ হওয়া rows filter করে, তারপর শুধু সেই rows-এর উপর AVG calculate করে।<2. HAVING ClauseHAVING clause groups of rows filter করে grouping এবং aggregation complete হওয়ার পর। এটি aggregate functions-এর result-এর উপর operate করে।<
  • Stage: GROUP BY এবং aggregate functions-এর পরে execute করে।
  • Scope: Aggregate results-এর উপর ভিত্তি করে groups filter করে।
  • Aggregate functions: ব্যবহার করতে পারে এবং সাধারণত করে।
  • Performance: Already aggregated data-এর উপর কাজ করে, তাই early row processing reduce করতে পারে না।
Example: শুধু সেই departments দেখানো যেখানে average salary 50,000-এর বেশি।
SELECT dept, AVG(salary) FROM employees GROUP BY dept HAVING AVG(salary) > 50000;
এখানে GROUP BY department groups তৈরি করে, AVG average calculate করে, এবং HAVING average কম হওয়া groups filter out করে।3. SQL-এ Execution OrderSQL query-তে যে order-এ clauses লেখা হয় সেই order-এ process করে না, বরং এই order-এ:
OrderClauseAction
1FROMSource table identify করা
2WHEREIndividual rows filter করা
3GROUP BYFiltered rows group করা
4AggregateSUM, COUNT, AVG ইত্যাদি apply করা
5HAVINGGrouped results filter করা
6SELECTDisplay-এর জন্য columns choose করা
7ORDER BYFinal output sort করা
4. Combined ExampleSales region-এর সেই departments খুঁজুন যেখানে total sales 100,000-এর বেশি।SELECT region, SUM(sales) AS total FROM orders WHERE region = 'Sales' GROUP BY region HAVING SUM(sales) > 100000;
  • WHERE region = ‘Sales’: শুধু Sales region-এর orders filter করে individual rows থেকে।
  • GROUP BY region: বাকি rows-কে region অনুযায়ী group করে।
  • SUM(sales): প্রতিটি region-এর total sales calculate করে।
  • HAVING SUM(sales) > 100000: শুধু সেই regions রাখে যেখানে total 100,000-এর বেশি।
Write an SQL query to retrieve the department name and the average salary from two tables named Department and Employee.Islami Bank,QA, 2025
SELECT d.dept_name, AVG(e.salary) AS average_salary
FROM department d
JOIN employee e ON d.dept_id = e.dept_id
GROUP BY d.dept_name;

Cost: Implementing and maintaining a DBMS can be expensive due to the cost of software licenses, hardware, and skilled personnel needed to manage the system.

Complexity: DBMS systems are complex to design, implement, and manage. They require specialized knowledge, and the complexity can increase with the size and scale of the database.

Scalability: Some DBMSs may face challenges when scaling to handle very large amounts of data or high transaction volumes efficiently, especially with unstructured data.

Dependence on Technology: Organizations become dependent on the specific DBMS technology they use, which can cause problems if the vendor stops support or if migration to another system is needed.

Performance Issues: Under heavy loads or with complex queries, DBMS performance can degrade, leading to slower response times and potential bottlenecks.

Data Integration: Integrating data from multiple, heterogeneous sources into a single DBMS can be difficult and time-consuming, especially when data formats and structures differ.

Security: Although DBMSs provide security features, they can still be vulnerable to unauthorized access, hacking, and data breaches if not properly managed.

Data Loss: In cases of hardware failure, software bugs, or disasters, there is a risk of losing data if proper backup and recovery mechanisms are not in place.

১. ব্যয় (Cost):
DBMS বাস্তবায়ন ও রক্ষণাবেক্ষণ ব্যয়বহুল, কারণ এতে সফটওয়্যার লাইসেন্স, হার্ডওয়্যার এবং দক্ষ জনবলের প্রয়োজন হয় ।

২. জটিলতা (Complexity):
DBMS সিস্টেম ডিজাইন, বাস্তবায়ন ও পরিচালনা করা জটিল। এটি ব্যবহারের জন্য বিশেষ জ্ঞানের প্রয়োজন হয়, এবং ডেটাবেসের আকার ও পরিধি বাড়লে জটিলতাও বৃদ্ধি পায়।

৩. স্কেলযোগ্যতা (Scalability):
অনেক সময় DBMS বড় পরিমাণ ডেটা বা উচ্চ ট্রানজেকশন হ্যান্ডল করতে গিয়ে সমস্যার সম্মুখীন হয়, বিশেষ করে যদি ডেটা আনস্ট্রাকচার্ড হয়।

৪. প্রযুক্তির উপর নির্ভরতা (Dependence on Technology):
একটি প্রতিষ্ঠান নির্দিষ্ট DBMS প্রযুক্তির উপর নির্ভরশীল হয়ে পড়ে। ফলে, যদি সেই ভেন্ডর সাপোর্ট বন্ধ করে দেয় বা অন্য সিস্টেমে স্থানান্তর করতে হয়, তখন সমস্যা দেখা দিতে পারে।

৫. পারফরম্যান্স সমস্যা (Performance Issues):
DBMS-এ ভারী লোড বা জটিল কোয়েরি থাকলে সিস্টেমের পারফরম্যান্স কমে যেতে পারে, যার ফলে রেসপন্স টাইম বেড়ে যায় এবং বটলনেক তৈরি হয়।

৬. ডেটা ইন্টিগ্রেশন (Data Integration): বিভিন্ন উৎস থেকে ডেটা একত্র করা কঠিন ও সময়সাপেক্ষ, বিশেষ করে যখন ডেটার ফরম্যাট ও গঠন এক হয়না।

৭. নিরাপত্তা (Security):
DBMS-এ নিরাপত্তা ব্যবস্থা থাকলেও, সঠিকভাবে পরিচালনা না করলে এটি অননুমোদিত প্রবেশ, হ্যাকিং বা ডেটা চুরির ঝুঁকিতে থাকে।

৮. ডেটা হারানো (Data Loss):
হার্ডওয়্যার ত্রুটি, সফটওয়্যার বাগ বা প্রাকৃতিক দুর্যোগের কারণে ডেটা হারানোর আশঙ্কা থাকে যদি সঠিক ব্যাকআপ ও রিকভারি ব্যবস্থা না থাকে।

The features of SQL (RDBMS), NoSQL, and NewSQL,

Given the following two tables (Students and Marks) in a database, write down the output of the given SQL queries and write down the SQL queries for the outputs:

i) SELECT Count (*) FROM Students S LEFT JOIN Marks M;
ii) SELECT StudentName From Students S JOIN Marks M ON S.Studentld = M.StudentId GROUP BY S.Studentld, S.StudentName HAVING SUM (Mark)>=200;
iii)List all the students name and number of subjects they have completed.
iv)List all the students who have not completed any subject.
v) List all the subject names. [Ministry of Food, Network/Website Manager(ICT),2024]
-- i) Query:
SELECT COUNT(*)
FROM Students S
LEFT JOIN Marks M
ON S.StudentId = M.StudentId;

-- Output:
-- 10
--
-- Explanation:
-- Student 1 -> 3 rows
-- Student 2 -> 3 rows
-- Student 3 -> 3 rows
-- Student 4 -> 1 row (no marks, but LEFT JOIN keeps it)
-- Total = 3 + 3 + 3 + 1 = 10


-- ii) Query:
SELECT StudentName
FROM Students S
JOIN Marks M
ON S.StudentId = M.StudentId
GROUP BY S.StudentId, S.StudentName
HAVING SUM(Mark) >= 200;

-- Output:
-- Mr. A
-- Mr. B
--
-- Explanation:
-- Mr. A = 70 + 50 + 80 = 200
-- Mr. B = 90 + 60 + 70 = 220
-- Mr. C = 30 + 70 + 60 = 160


-- iii) List all the students name and number of subjects they have completed
SELECT S.StudentName, COUNT(M.Subject) AS NumberOfSubjects
FROM Students S
LEFT JOIN Marks M
ON S.StudentId = M.StudentId
GROUP BY S.StudentId, S.StudentName;

-- Output:
-- Mr. A   3
-- Mr. B   3
-- Mr. C   3
-- Mr. D   0


-- iv) List all the students who have not completed any subject
SELECT S.StudentName
FROM Students S
LEFT JOIN Marks M
ON S.StudentId = M.StudentId
WHERE M.StudentId IS NULL;

-- Output:
-- Mr. D


-- v) List all the subject names
SELECT DISTINCT Subject
FROM Marks;

-- Output:
-- Math
-- Bengali
-- Physics

See the following Table:
Employees(EMP_ID, EMP_Name, Manager_ID, Dept_ID);
Departments(Dept_ID, Salary, Dept_Name, Emp_ID); Submarine Cables ,AM (Engineering), 2024

Find out the names of manager of each employee.
SELECT e.EMP_Name AS Employee,
       m.EMP_Name AS Manager
FROM Employees e
LEFT JOIN Employees m
ON e.Manager_ID = m.EMP_ID;
Sort the employees of each department based on salary in descending order
SELECT e.EMP_Name, d.Dept_Name, d.Salary
FROM Employees e
JOIN Departments d
ON e.EMP_ID = d.Emp_ID
ORDER BY d.Dept_ID, d.Salary DESC;
Suppose that we have a relational database with the following table. Underlined one represent primary key

Movies (mid, title, year)
People (pid, name)
Genres (gid, genre)
HasRole (pid, mid, role)
Has Genre (gid, mid)

Write a SQL query to return the number of movies that are romantic comedies.BB, (AP)-2023

SELECT COUNT(*)
FROM Movies, Genres, HasGenre
WHERE Movies.mid = HasGenre.mid
AND HasGenre.gid = Genres.gid
AND Genres.genre = “romantic comedy”;

Suppose we have a relational database with five tables. table key Attributes S(sid, A)Sid T(tid, B) Tid U(uid, C) Uid R(sid, tid, D) sid, tid Q(tid, uid, E) tid, uid Here R implements a many-to-many relationship between the entities implemented with tables S and T, and Q implements a many-to-many relationship between the entities implemented with tables T and U CB, AP, 23.

We need to retrieve sid from table R and uid from table Q, where the two tables are related through the common column tid.

SELECT R.sid, Q.uid
FROM R
JOIN Q ON R.tid = Q.tid;

Explanation:

  • R.sid: Selects the sid from table R.
  • Q.uid: Selects the uid from table Q.
  • The JOIN connects R and Q through their tid columns, which establish the relationship between the two tables.

Here, we need to select A from table S and C from table U, connecting them through the relations R and Q.

SELECT S.A, U.C
FROM S
JOIN R ON S.sid = R.sid
JOIN Q ON R.tid = Q.tid
JOIN U ON Q.uid = U.uid;

Explanation:

  • S.A: Selects the A-value from the S table (which corresponds to sid).
  • The first JOIN connects table S with table R through sid.
  • The second JOIN connects R and Q on tid.
  • Finally, Q is connected to U via uid, allowing us to select C from U.
Consider the following relation:
Sales(sales_id, salesman, region, sale_amount, sale_date)
,
Write an SQL query to display the region, average sale amount, and total number of sales for each region where: The average sale amount exceeds BDT 50,000 and the total number of sales in that region is at least 5.CB, SO(it), 2025

SQL Query
The following query displays the region, average sale amount, and total number of sales for each region where the average sale amount is greater than BDT 50,000 and the total number of sales is at least 3.

SELECT region,
       AVG(sale_amount) AS avg_sale_amount,
       COUNT(*) AS total_sales
FROM Sales
GROUP BY region
HAVING AVG(sale_amount) > 50000
   AND COUNT(*) >= 3;

SQL Query
নিচের query টি প্রতিটি region অনুযায়ী average sale amount এবং total sales সংখ্যা দেখায়, যেখানে average sale amount BDT 50,000 এর বেশি এবং ঐ region এ মোট sales কমপক্ষে 3টি

SELECT region,
       AVG(sale_amount) AS avg_sale_amount,
       COUNT(*) AS total_sales
FROM Sales
GROUP BY region
HAVING AVG(sale_amount) > 50000
   AND COUNT(*) >= 3;
Let a database has two tables, Customers and Orders. The following figure shows the partial data of these two tables. Based on this partial data, explain Inner, Left, Right and Full join. Show the result set of each join operation
CB, SO(IT), 2024

Inner Join:

An Inner Join returns only the rows that have matching values in both tables.

SELECT Customers.ID, Customers.FirstName, Orders.OrderID, Orders.Amount
FROM Customers
INNER JOIN Orders ON Customers.ID = Orders.CustomerID;

Result Set:

IDFirst NameOrder IDAmount
3Belal2500
5Helal4800

Left Join (or Left Outer Join):

A Left Join returns all rows from the left table (Customers), and the matched rows from the right table (Orders). If there is no match, the result is NULL on the right side.

SELECT Customers.ID, Customers.FirstName, Orders.OrderID, Orders.Amount
FROM Customers
LEFT JOIN Orders ON Customers.ID = Orders.CustomerID;

Result Set:

IDFirst NameOrder IDAmount
1RahimNULLNULL
2KarimNULLNULL
3Belal2500
4RonyNULLNULL
5Helal4800

Right Join (or Right Outer Join):

A Right Join returns all rows from the right table (Orders), and the matched rows from the left table (Customers). If there is no match, the result is NULL on the left side.

SELECT Customers.ID, Customers.FirstName, Orders.OrderID, Orders.Amount
FROM Customers
RIGHT JOIN Orders ON Customers.ID = Orders.CustomerID;

Result Set:

IDFirst NameOrder IDAmount
NULLNULL1200
3Belal2500
NULLNULL3300
5Helal4800
NULLNULL5150

Full Join (or Full Outer Join):

A Full Join returns all rows when there is a match in either the left (Customers) or right (Orders) table. Rows without a match in one of the tables will have NULLs in the corresponding columns.

SELECT Customers.ID, Customers.FirstName, Orders.OrderID, Orders.Amount
FROM Customers
FULL OUTER JOIN Orders ON Customers.ID = Orders.CustomerID;

Result Set:

IDFirst NameOrder IDAmount
1RahimNULLNULL
2KarimNULLNULL
3Belal2500
4RonyNULLNULL
5Helal4800
NULLNULL1200
NULLNULL3300
NULLNULL5150

Below tables are given, Employee (employee_id, name, salary, department) Leave (employee_id, date, reason, no_leaves) Holiday (Date, description) Rupali Bank, ANE, 2022

(i) Write mapping cardinality between 'Employee' and 'Holiday' table.

(ii) Write query to show all employee's leave count.
SELECT employee_id, COUNT(employee_id)
FROM Leave
GROUP BY employee_id;
(iii) Write query to show employees who are in 'HR' department and have taken at least 5 leaves.
SELECT *
FROM employee
WHERE employee_id IN (
    SELECT employee_id
    FROM leave
    GROUP BY employee_id
    HAVING COUNT(employee_id) > 5
);
Analize the following code:

SELECT d.department_name, AVG(e.salary) AS average_salary
FROM employees e
JOIN department d ON e.department_id = d.department_id
WHERE e.salary > (SELECT AVG(salary) FROM employees)
GROUP BY d.department_name
HAVING COUNT(*) > 2
ORDER BY average_salary DESC;

Sonali and Janata Bank , O(it), 2023

SELECT d.department_name, AVG(e.salary) AS average_salary

This clause selects the department name and calculates the average salary of employees in each department.

FROM employees e

This clause specifies the employees table as the main data source and uses e as an alias.

JOIN department d ON e.department_id = d.department_id

This join links employees with their departments using the common department_id.

WHERE e.salary > (SELECT AVG(salary) FROM employees)

This condition filters employees whose salary is higher than the overall average salary of all employees.

GROUP BY d.department_name

This clause groups records by department name to calculate aggregate values.

HAVING COUNT(*) > 2

This condition ensures that only departments with more than two employees are included.

ORDER BY average_salary DESC

This clause sorts the result in descending order based on average salary.

SELECT d.department_name, AVG(e.salary) AS average_salary

এই clause department-এর নাম দেখায় এবং প্রতিটি department-এর employee-দের average salary হিসাব করে।

FROM employees e

এই clause employees table-কে data source হিসেবে ব্যবহার করে এবং e alias ব্যবহার করে।

JOIN department d ON e.department_id = d.department_id

এই JOIN clause department_id ব্যবহার করে employee-দের তাদের department-এর সাথে যুক্ত করে।

WHERE e.salary > (SELECT AVG(salary) FROM employees)

এই condition শুধুমাত্র সেই employee-দের নির্বাচন করে যাদের salary সব employee-এর overall average salary-এর চেয়ে বেশি।

GROUP BY d.department_name

এই clause department অনুযায়ী data group করে aggregate calculation করতে সাহায্য করে।

HAVING COUNT(*) > 2

এই clause নিশ্চিত করে যে শুধুমাত্র ২ জনের বেশি employee থাকা department গুলো result-এ আসবে।

ORDER BY average_salary DESC

এই clause average salary অনুযায়ী descending order-এ result সাজায়।

Analyze the output of the following SQL :
SELECT department_name, AVG(salary) AS average_salary
FROM employees e
JOIN departments d ON e.department_id = d.department_id
WHERE salary > (
SELECT AVG(salary)
FROM employees
WHERE department_id = d.department_id
)
GROUP BY department_name
HAVING COUNT(*) > 2
ORDER BY average_salary DESC;
Rupali , ANE, 2023

Explanation of SQL Query Clauses

SELECT Clause: The SELECT clause specifies which columns or calculated values will appear in the result.

  • department_name selects the department name from the departments table.
  • AVG(salary) AS average_salary calculates the average salary of selected employees and renames it as average_salary.

FROM Clause: The FROM clause specifies the tables used in the query.

  • employees e selects the employees table with alias e.
  • JOIN departments d ON e.department_id = d.department_id joins employees with departments using department_id.

WHERE Clause: The WHERE clause filters rows before grouping.

  • salary > (SELECT AVG(salary) FROM employees WHERE department_id = d.department_id) selects employees whose salary is greater than the average salary of their department.

GROUP BY Clause: The GROUP BY clause groups rows to perform aggregate calculations.

  • GROUP BY department_name groups employees by department name.

HAVING Clause: The HAVING clause filters groups after aggregation.

  • HAVING COUNT(*) > 2 selects only departments with more than two qualifying employees.

ORDER BY Clause: The ORDER BY clause sorts the final result.

  • ORDER BY average_salary DESC sorts departments by average salary in descending order.

SQL Query Clause-এর ব্যাখ্যা

SELECT Clause: SELECT clause নির্ধারণ করে কোন column বা calculated value result-এ দেখানো হবে।

  • department_name departments table থেকে department-এর নাম নির্বাচন করে।
  • AVG(salary) AS average_salary employee-দের average salary হিসাব করে এবং নাম দেয় average_salary

FROM Clause: FROM clause নির্ধারণ করে কোন table থেকে data নেওয়া হবে।

  • employees e employees table ব্যবহার করে, যেখানে e হলো alias।
  • JOIN departments d ON e.department_id = d.department_id department_id ব্যবহার করে দুইটি table যুক্ত করে।

WHERE Clause: WHERE clause grouping-এর আগে row filter করে।

  • salary > (SELECT AVG(salary) FROM employees WHERE department_id = d.department_id) শুধুমাত্র সেই employee নির্বাচন করে যাদের salary তাদের department-এর average salary-এর চেয়ে বেশি।

GROUP BY Clause: GROUP BY clause aggregate calculation করার জন্য data group করে।

  • GROUP BY department_name department অনুযায়ী employee group করে।

HAVING Clause: HAVING clause aggregation-এর পরে group filter করে।

  • HAVING COUNT(*) > 2 শুধুমাত্র সেই department দেখায় যেখানে ২ জনের বেশি qualifying employee আছে।

ORDER BY Clause: ORDER BY clause final result sort করে।

  • ORDER BY average_salary DESC average salary অনুযায়ী descending order-এ result দেখায়।

Write SQL command from the following tables.
Employee (ename, street, city)
Works (ename, cname, salary, joindate)
Company (cname, city)
Manages (ename, mname) 6 Bank & FI, AP, 2021

(a) Find name, street, city who work for First Corporation Bank and earn more than 30000
SELECT e.ename, e.street, e.city
FROM Employee e, Works w
WHERE e.ename = w.ename
  AND w.cname = 'First Corporation Bank'
  AND w.salary > 30000;
(b) Find name of all employees, who live in the same city and company for which they work.
SELECT e.ename
FROM Employee e, Works w, Company c
WHERE e.city = c.city
  AND w.cname = c.cname
  AND e.ename = w.ename
  AND e.city = w.city;
(c) Give all employees of First Century Bank 10 percent salary raise.
UPDATE Works
SET salary = salary + salary * 0.1
WHERE cname = 'First Century Bank';
(d) Find the company with payroll less than 100000.
SELECT cname
FROM Works
GROUP BY cname
HAVING SUM(salary) < 100000;
Coming Soon
WhatsApp Telegram Messenger