Working with SQL Databases
Hands On Practice
(Part-III)
By: Faizan Irshad
SQL - INNER JOIN Query
The INNER JOIN query is used to retrieve the matching records from
two or more tables based on the specified condition.
Syntax:
SELECT table1.column_name(s), table2.column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
For the demo purpose, we will use the
following Employee and Department tables in all examples.
Department Table
DeptId Name
1 Finance
2 HR
3 IT
4 Sales
Update existing data in the Department table
UPDATE Department SET Name = 'Finance' WHERE DeptId = 1;
UPDATE Department SET Name = 'HR' WHERE DeptId = 2;
Insert new data into the Department table
INSERT INTO Department (DeptId, Name) VALUES (3, 'IT');
INSERT INTO Department (DeptId, Name) VALUES (4, 'Sales');
By: Faizan Irshad
Employee Table
EmpId FirstName LastName Email PhoneNo Salary DeptId
2 Arif Baig NULL NULL NULL NULL
5 Amit Patel NULL NULL 18000 4
To delete all values from Employee Table
Delete from Employee
To add new data to Employee Table
INSERT INTO Employee VALUES
(1, 'Ali', 'Javed', '
[email protected]', NULL, 33000, 1),
(2, 'Arif', 'Baig', NULL, NULL, NULL, NULL),
(3, 'Nauman', 'Ahsan', '
[email protected]', NULL, 17000, 2),
(4, 'Iqra', 'Sabahat', '
[email protected]', NULL, 15000, 1),
(5, 'Amit', 'Patel', NULL, NULL, 18000, 4),
(6, 'Abdul', 'Kalam', '
[email protected]', NULL, 25000, 2);
Select * from Employee
Consider the following inner join query.
SQL Script: Inner Join Query
SELECT Employee.EmpId, Employee.FirstName,
Employee.LastName, Department.Name
FROM Employee
INNER JOIN Department
ON Employee.DeptId = Department.DeptId;
The above inner join query joins the Employee table
and Department table and retrieves records from both the tables
By: Faizan Irshad
where Employee.DeptId = Department.DeptId . It only fetches records
from both the tables where DeptId in the Employee table matches with
the DeptId of the Department table. If the DeptId is NULL or not
matching, then it won't retrieve those records. The following is the
result of the above query.
Employee Table
EmpId FirstName LastName Name
1 Ali Javed Finance
3 Nauman Ahsan HR
4 Iqra Sabahat Finance
5 Amit Patel Sales
6 Abdul Kalam HR
Notice that it only displayed the records whose DeptId matches, not
whose DeptId is null or not matching.
It does not matter which table you take first in the query. The following
query will display the same result as above.
SQL Script: Inner Join Query
SELECT Employee.EmpId, Employee.FirstName,
Employee.LastName, Department.Name
FROM Department
INNER JOIN Employee
ON Department.DeptId = Employee.DeptId;
It is not mandatory to use the INNER JOIN phrase. You can use the
WHERE clause to achieve the same result, as shown below.
SQL Script: Inner Join using WHERE Clause
SELECT Employee.EmpId, Employee.FirstName,
Employee.LastName, Department.Name
FROM Department, Employee
WHERE Department.DeptId = Employee.DeptId;
By: Faizan Irshad
You can take alias as short cuts of table names, as shown below.
SQL Script: Inner Join Query
SELECT emp.EmpId, emp.FirstName, emp.LastName, dept.Name
FROM Department dept
INNER JOIN Employee emp
ON dept.DeptId = emp.DeptId;
Repeat the INNER JOIN.. ON statement to include one more tables in the
query. For example, the following inner join query joins the three
tables. Will work once Consultant Table is also created.
SQL Script: Inner Join Query
SELECT Employee.EmpId, Employee.FirstName,
Employee.LastName, Consultant.FirstName, Department.Name
FROM Employee
INNER JOIN Department
ON Employee.DeptId = Department.DeptId
INNER JOIN Consultant
ON Consultant.DeptId = Department.DeptId;
SQL - LEFT JOIN Query
The LEFT JOIN is a type of inner join where it returns all the records from the left table
and matching records from the right table. Here, the left table is a table that comes to the
left side or before the "LEFT JOIN" phrase in the query, and the right table refers to a
table that comes at the right side or after the "LEFT JOIN" phrase. It returns NULL for
all non-matching records from the right table.
In some databases, it is called LEFT OUTER JOIN.
Syntax:
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
By: Faizan Irshad
In the above syntax, table1 is the left table and table2 is the right table.
Consider the following left join query.
SQL Script: Left Join Query
SELECT emp.empid, emp.FirstName, dept.DeptId, dept.Name
FROM Employee emp
LEFT JOIN Department dept
ON emp.DeptId = dept.DeptId;
The above LEFT JOIN query joins the Employee table and Department table
where Employee is the left table and Department is the right table. It retrieves all the
records from the Employee table and matching records from the Department table
where emp.DeptId = dept.DeptId . The above query will display the following result.
EmpId FirstName DeptId Name
1 Ali 1 Finance
2 Arif NULL NULL
3 Nauman 2 HR
4 Iqra 1 Finance
5 Amit 4 Sales
6 Abdul 2 HR
Notice that it only displayed the records from the Department table
whose dept.DeptId matches with the emp.DeptId for remaining it gave Null
values.
Now, let's change the left table and see how the result will be changed.
SQL Script: Left Join Query
SELECT emp.empid, emp.FirstName, dept.DeptId, dept.Name
FROM Department dept
LEFT JOIN Employee emp
ON dept.DeptId = emp.DeptId;
Above, left table is Department and the right table is the Employee. This query will
display the following result.
By: Faizan Irshad
EmpId FirstName DeptId Name
1 Ali 1 Finance
4 Iqra 1 Finance
3 Nauman 2 HR
6 Abdul 2 HR
NULL NULL 3 IT
5 Amit 4 Sales
As you can see, it fetches all the records from the Department table and only matching
records from the Employee table where dept.DeptId = emp.DeptId . It contains the 3,
'IT' record from the Department table and NULL for Employee columns because
there are no matching records in the Employee table whose DeptId is 3.
SQL - RIGHT JOIN Query
The RIGHT JOIN is the reverse of LEFT JOIN. The RIGHT JOIN query
returns all the records from the right table and matching records from
the left table.
Here, the right side table is a table that comes to the right side or after
the "RIGHT JOIN" phrase in the query, and the left table is a table that
comes at the left side or before the "RIGHT JOIN" phrase.
The RIGHT JOIN returns NULL for all non-matching records from the
left table. In some databases, it is called RIGHT OUTER JOIN.
Syntax:
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
In the above syntax, table2 is the right table and table1 is the left
table.
Now, look at the following RIGHT JOIN query.
By: Faizan Irshad
SQL Script: RIGHT JOIN Query
SELECT dept.DeptId, dept.Name, emp.empid, emp.FirstName
FROM Employee emp
RIGHT JOIN Department dept
ON emp.DeptId = dept.DeptId;
The above RIGHT JOIN query joins the Employee table
and Department table where Employee is the left table and Department is
the right table. The above query will display the following result.
DeptId Name EmpId FirstName
1 Finance 1 Ali
1 Finance 4 Iqra
2 HR 3 Nauman
2 HR 6 Abdul
3 IT NULL NULL
4 Sales 5 Amit
As you can see, it fetches all the records from the Department table and
only matching records from the Employee table where emp.DeptId =
dept.DeptId.
Now, let's change the right table and see how the result will be
changed.
SQL Script: RIGHT Join Query
SELECT emp.empid, emp.FirstName, dept.DeptId, dept.Name
FROM Department dept
RIGHT JOIN Employee emp
ON dept.DeptId = emp.DeptId;
Above, the left table is Department and the right table is the Employee.
This query will display the following result.
By: Faizan Irshad
EmpId FirstName DeptId Name
1 Ali 1 Finance
2 Arif NULL NULL
3 Nauman 2 HR
4 Iqra 1 Finance
5 Amit 4 Sales
6 Abdul 2 HR
As you can see, it displays all the records from the right
table Employee and matching rows from the left table Department. It
returns NULL for all non-matching records from the Employee table.
SQL - FULL JOIN Query
The FULL JOIN returns all the records of all the specified tables. It
includes NULL for any non-matching records.
In some databases, FULL JOIN is called FULL OUTER JOIN. It can return
a very large result set because it returns all the rows from all the
tables.
Syntax:
SELECT column_name(s)
FROM table1
FULL JOIN table2
ON table1.column_name = table2.column_name;
Now, look at the following FULL JOIN query.
SQL Script: FULL JOIN Query
SELECT emp.EmpId, emp.FirstName, dept.DeptId, dept.Name
FROM Employee emp
FULL JOIN Department dept
ON emp.DeptId = dept.DeptId;
By: Faizan Irshad
The above query will display the following result.
DeptId Name EmpId FirstName
1 Ali 1 Finance
2 Arif NULL NULL
3 Nauman 2 HR
4 Iqra 1 Finance
5 Amit 4 Sales
6 Abdul 2 HR
NULL NULL 3 IT
As you can see, it fetches all the records from both tables. First, it
fetches all the records from the left table Employee and then all the
records from the right table Department. It includes NULL for all non-
matching records from both tables.
Now, let's swap the tables and see how the result will be changed.
SQL Script: FULL Join Query
SELECT emp.EmpId, emp.FirstName, dept.DeptId, dept.Name
FROM Department dept
FULL JOIN Employee emp
ON emp.DeptId = dept.DeptId;
The above query will display the following result.
EmpId FirstName DeptId Name
1 Ali 1 Finance
4 Iqra 1 Finance
3 Nauman 2 HR
6 Abdul 2 HR
NULL NULL 3 IT
By: Faizan Irshad
EmpId FirstName DeptId Name
5 Amit 4 Sales
2 Arif NULL NULL
As you can see, first it displays all the records from the right
table Department and then records from the left table Employee. It
returns NULL for all non-matching records in both tables.
SQL - BETWEEN Operator
The BETWEEN operator is used in the WHERE conditions to filter
records within the specified range. The range of values can be strings,
numbers, or dates. The range of values must be specified with the AND
operator, as shown below.
Syntax:
SELECT column1, column2,..
FROM table
WHERE column BETWEEN begin_value AND end_value
For the demo purpose, we will use the following Employee tables in all
examples.
Consider the following query.
SQL Script: BETWEEN Operator
SELECT EmpId, FirstName, LastName, Salary
FROM Employee
WHERE Salary BETWEEN 10000 AND 20000;
Above, the Salary column is used with the BETWEEN operator to filter
records. The Salary BETWEEN 10000 AND 20000; specifies that the values
in the Salary column should be between 10000 and 20000 (inclusive
of both values). The above query will display the following result.
By: Faizan Irshad
EmpId FirstName LastName Salary
3 'Nauman' 'Ahsan' 17000
4 'Iqra' 'Sabahat' 15000
5 'Amit' 'Patel' 18000
Note that you must use the AND operator with the BETWEEN operator;
otherwise, it will raise an error.
BETWEEN String Range
The following query uses the string range with the BETWEEN operator.
SQL Script: BETWEEN Operator
SELECT * FROM Employee
WHERE LastName BETWEEN 'a%' AND 'j%';
Above, string range LastName BETWEEN 'a%' AND 'j%' fetches records
where the LastName value should start from either 'a' or any character
before 'j' (but not 'j'). It will display the following result.
EmpId FirstName LastName Email Phone No Salary DeptID
2 Arif Baig NULL NULL NULL NULL
NOT BETWEEN
Use the NOT operator with the BETWEEN operator to filter records that
do not fall in the specified range.
SQL Script: BETWEEN Operator
SELECT EmpId, FirstName, LastName, Salary
FROM Employee
WHERE Salary NOT BETWEEN 10000 AND 20000;
By: Faizan Irshad
Above, the Salary column is used with the BETWEEN operator to filter
records. The Salary NOT BETWEEN 10000 AND 20000; specifies that the
value in the Salary column should not be between 10000 and 20000.
The above query will display the following result.
EmpId FirstName LastName Salary
1 Ali Javed 33000
6 Abdul Kalam 25000
SQL - IN Operator
The IN operator is used to specify the list of values or sub query in the
WHERE clause. A sub-query or list of values must be specified in the
parenthesis e.g. IN (value1, value2, ...) or IN (Select query) .
Syntax:
SELECT column1, column2,..
FROM table
WHERE column IN (value1, value2, value3,...)
-- or
SELECT column1, column2,..
FROM table
WHERE column IN (SELECT query)
Consider the following query.
SQL Script: IN Operator
SELECT EmpId, FirstName, LastName, Salary
FROM Employee
WHERE EmpId IN (1, 3, 5, 6)
The above query will return records where EmpId is 1 or 3 or 5 or 6.
The above query will display the following result.
By: Faizan Irshad
EmpId FirstName LastName Salary
1 Ali Javed 33000
3 Nauman Ahsan 17000
5 Amit Patel 18000
6 Abdul Kalam 25000
The following query uses the string values with the IN operator.
SQL Script: IN Operator
SELECT EmpId, FirstName, LastName, Salary
FROM Employee
WHERE FirstName IN ('Arif','abdul');
EmpId FirstName LastName Salary
2 Arif Baig NULL
6 Abdul Kalam 25000
Note that wildcard characters '%', '_', etc. cannot be used with the
string values in this IN operator.
Sub-query with IN Operator
You can use the subquery with the IN operator that returns records
from the single column. The subquery cannot include more than one
column in the SELECT column list.
SQL Script: IN Operator
SELECT EmpId, FirstName, LastName, DeptId
FROM Employee
WHERE DeptId IN (SELECT DeptId from Department WHERE DeptId > 3);
In the above query, the sub-query SELECT DeptId from Department WHERE
DeptId > 3 returns DeptId, 4. So, now the query would be like SELECT
By: Faizan Irshad
EmpId, FirstName, LastName, Salary FROM Employee WHERE DeptId in (4); .
The following is the result.
EmpId FirstName LastName DeptId
5 Amit Patel 4
NOT IN
Use the NOT operator with the IN operator to filter records that do not
fall in the specified values.
SQL Script: NOT IN Operator
SELECT EmpId, FirstName, LastName, Salary
FROM Employee
WHERE EmpId NOT IN (1, 3, 5);
EmpId FirstName LastName Salary
2 Arif Baig NULL
4 Iqra Sabahat 15000
6 Abdul Kalam 25000
SQL - LIKE Operator
The LIKE operator is used in the WHERE condition to filter data based
on some specific pattern. It can be used with numbers, string, or date
values. However, it is recommended to use the string values.
The LIKE operator in MS SQL Server, SQLite, MySQL database are not
case-sensitive, whereas it is case-sensitive in Oracle, and PostgreSQL
database.
By: Faizan Irshad
Syntax:
SELECT * FROM table_name
WHERE column_name LIKE 'pattern'
The LIKE operator uses the following wildcard characters to specify a
pattern:
Pattern Description
% The % matches zero, one, or multiple characters (capital or small) or numbers.
E.g. 'A%' will match all string starting with 'A' and followed by any number of characters or numbers.
_ The underscore _ sign matches any single character or number.
E.g. 'A_' will match all strings with two chars where the first character must be 'A' and second character can be
anything.
[] The [] matches any single character within the specified range in the [].
E.g. 'A[e,l,p]' will match 'Apple', 'Aelp', 'Alep', 'Aple', etc.
[^] The [^] matches any single character except the specified range in the [^]. E.g. 'A[^e,l,p]' will match anything
that starts with 'A', but not 'Apple', 'Aelp', 'Alep', 'Aple', etc.
These wildcard characters can be used individually or in combination
with each other in the LIKE operator.
Consider the following query with the LIKE operator.
The wildcard char % specifies any number of characters.
SQL Script: Wildcard %
SELECT * FROM Employee
WHERE FirstName LIKE 'a%';
The above query fetches all the records where the value of
the FirstName column starts with either 'a' or 'A' followed by any number
of characters.
By: Faizan Irshad
EmpId FirstName LastName Email Phone No Salary DeptID
2 Arif Baig NULL NULL NULL NULL
5 Amit Patel NULL NULL 18000 4
The following query retrieves data where LastName value is '%a%'. It
means the value must contain 'a' in any position.
SQL Script: IN Operator
SELECT * FROM Employee
WHERE LastName LIKE '%a%';
The above query will display the following result.
EmpId FirstName LastName Email Phone No Salary DeptID
2 Arif Baig NULL NULL NULL NULL
5 Amit Patel NULL NULL 18000 4
The following table list common patterns used with the LIKE operator:
Pattern Description
FirstName LIKE Ali Returns records whose FirstName value is Ali or Ali or Ali or Ali or Ali.
FirstName LIKE 'j%' Returns records whose FirstName value starts with 'j' or 'J' followed by any number of
characters or numbers.
FirstName LIKE '%a%' Returns records whose FirstName value contains 'a' or 'A' at any position.
By: Faizan Irshad
Pattern Description
FirstName LIKE 'a%b' Returns records whose FirstName value should start with 'a' or 'A' and last character
should be 'b' or 'B'.
FirstName LIKE '_a' Returns records whose FirstName value contains two characters and the second
character must be 'a' or 'A'.
FirstName LIKE '_a%' Returns records whose FirstName value contains the second characters 'a' or 'A'.
FirstName LIKE '%a_' Returns records whose FirstName value has the second last character is either 'a' or 'A'.
FirstName LIKE '___' Returns records whose FirstName value must be three characters long.
FirstName LIKE '___%' Returns records whose FirstName value must contain at least three characters or more.
FirstName LIKE 'A[i,m,t]' Returns records whose FirstName value starts from 'A' and followed by any characters
specified in [].
FirstName LIKE 'A[^i,m,t]' Returns records whose FirstName value starts from 'A' and should not followed by any
characters specified in [].
NOT LIKE
Use the NOT operator with the LIKE operator to filter records that do
not match with the specified string.
SQL Script: NOT IN Operator
SELECT * FROM Employee
WHERE LastName NOT LIKE 'j%';
Above, LastName NOT LIKE 'j%' retrieves records where the LastName values
not start with 'j'.
EmpId FirstName LastName Email Phone No Salary DeptID
2 Arif Baig NULL NULL NULL NULL
5 Amit Patel NULL NULL 18000 4
By: Faizan Irshad
EmpId FirstName LastName Email Phone No Salary DeptID
SQL - Select Distinct Records
A column in the database table may contain duplicate values. The
SELECT DISTINCT statement is used to retrieve unique values of a
column or combination of one or more columns.
Syntax:
SELECT DISTINCT column_name1, column_name2,...
FROM table_name
[WHERE Condition];
The following query returns unique records from the Employee table.
SQL Script: Select Distinct Records
SELECT DISTINCT * FROM Employee;
The above query returns all the unique records even if at least one
column has a unique value.
EmpId FirstName LastName Email Phone No Salary DeptID
2 Arif Baig NULL NULL NULL NULL
5 Amit Patel NULL NULL 18000 4
The following query retrieves the unique FirstName values.
SQL Script: Distinct Columns
By: Faizan Irshad
SELECT DISTINCT DeptID FROM Employee;
DeptID
NULL
The following query retrieves the unique combination
of FirstName and LastName values.
SQL Script: Distinct Columns
SELECT DISTINCT FirstName, LastName FROM Employee;
FirstName LastName
1 Ali Javed
2 Arif Baig
3 Nauman Ahsan
4 Iqra Sabahat
5 Amit Patel
6 Abdul Kalam
The following counts the unique EmpId.
SQL Script: Count Distinct Values
SELECT count(DISTINCT EmpId) FROM Employee;
By: Faizan Irshad
SQL - AVG() Function
The AVG() is an aggregate function that is used to find out an average
of the list of values of columns or an expression.
The AVG() function is used with the numeric columns only.
Syntax:
SELECT AVG(column_name)
FROM table_name
WHERE condition;
The following query calculates the average salary of all the employees.
SQL Script: AVG()
SELECT AVG(Salary) AS AVERAGE FROM Employee;
AVERAGE
21600
You can also calculate the average salary of each department using
the following command:
SQL Script: AVG()
SELECT DeptId, AVG(Salary) AS AVERAGE FROM Employee GROUP BY
DeptId;
DeptId AVERAGE
NULL NULL
1 24000
2 21000
4 18000
By: Faizan Irshad
SQL - COUNT() Function
The COUNT() function is an aggregate function that is used to find the
number of values in the specified column excluding NULL values. It
can be applied on numeric, character, or date values.
Syntax:
SELECT COUNT([ALL | DISTINCT] expression | column_name)
FROM table_name
[WHERE condition]
[GROUP BY] ;
Specify the column name to count the total values in that column
excluding NULL values. The following counts the total records in
the FirstName column.
SQL Script: COUNT()
SELECT COUNT(FirstName) AS "Total" FROM Employee;
Total
The COUNT() function excludes the NULL values.
SQL Script: COUNT()
SELECT COUNT(Email) AS "Total" FROM Employee;
Total
The COUNT() function can contain other functions such as Distinct, as
shown below.
SQL Script: COUNT()
SELECT COUNT(Distinct(LastName)) AS "Total" FROM Employee;
By: Faizan Irshad
Total
Use the COUNT(*) to count the total records in the table. The following
query count the total records of the Employee table.
SQL Script: COUNT(*)
SELECT COUNT(*) AS "Total Records" FROM Employee;
Total Records
The COUNT() is an aggregate function, so it can be used in Group
By queries. The following query counts the number of employees in
each department.
SQL Script: COUNT() with Group By
SELECT DeptId, COUNT(*) AS "Total Employees"
FROM Employee
GROUP BY DeptId;
DeptId Total Employees
NULL 1
1 2
2 2
4 1
By: Faizan Irshad
SQL MAX() function
The MAX() function is an aggregate function that is used to find the
largest value in the given column or expression. It can be applied on
the numeric, character, or date values.
Syntax:
SELECT MAX(column_name)
FROM table_name
[WHERE condition]
[GROUP BY];
The following selects the maximum salary from the Employee table.
SQL Script: MAX()
SELECT MAX(Salary) AS "Highest Salary" FROM Employee;
Highest Salary
33000
The following query gets employee whose salary is maximum.
SQL Script: MAX()
SELECT * FROM Employee
WHERE Salary = (SELECT MAX(Salary) FROM Employee);
EmpId FirstName LastName Email Phone No Salary DeptId
1 Ali Javed
[email protected] NULL 33000 1
The MAX() is an aggregate function, so it can be used in Group
By queries. The following query gets highest salary in each
department.
SQL Script: MAX() with Group By
SELECT DeptId, MAX(Salary) AS "Highest Salary"
FROM Employee
GROUP BY DeptId;
By: Faizan Irshad
DeptId Highest Salary
NULL NULL
1 33000
2 25000
4 18000
The MAX() function can be applied on the varchar columns. The
following selects the largest FirstName from the Employee table.
SQL Script: MAX() with varchar column
SELECT MAX(FirstName) AS "Largest FirstName" FROM
Employee;
Largest FirstName
Nauman
SQL - MIN() Function
The MIN() function is an aggregate function that is used to find the
smallest value of given column or expression. It can be applied on
numeric, character or date values.
Syntax:
SELECT MIN(column_name)
FROM table_name
[WHERE condition]
[GROUP BY];
The following selects the smallest salary from the Employee table.
SQL Script: MIN()
SELECT MIN(Salary) AS "Smallest Salary" FROM Employee;
Smallest Salary
15000
The following query gets all employees whose salary is the minimum.
SQL Script: MIN()
By: Faizan Irshad
SELECT * FROM Employee
WHERE Salary = (SELECT MIN(Salary) FROM Employee);
Phone
EmpId FirstName LastName Email No Salary DeptId
The MIN() is an aggregate function, so it can be used in Group
By queries. The following query gets Smallest salary in each
department.
SQL Script: MIN() with Group By
SELECT DeptId, MIN(Salary) AS "Smallest Salary"
FROM Employee
GROUP BY DeptId;
DeptId Smallest Salary
NULL NULL
1 15000
2 17000
4 18000
The MIN() function can be applied on the varchar columns. The
following selects the smallest FirstName from the Employee table in
alphabetic manner.
SQL Script: MIN()
SELECT MIN(FirstName) AS "Smallest FirstName" FROM
Employee;
Smallest FirstName
Abdul
By: Faizan Irshad
SQL - SUM Function
The SUM() function is an aggregate function that is used to find the
sum (addition) of the given column or an expression. It can be applied
on the numeric values or numeric columns only.
Syntax:
SELECT SUM(column_name)
FROM table_name
[WHERE condition];
The following query calculates the total salary of all the employees.
SQL Script: SUM()
SELECT SUM(Salary) AS "Total Salary" FROM Employee;
Total Salary
108000
You can also calculate the total salary of each department using the
following query:
SQL Script: SUM()
SELECT DeptId, SUM(Salary) AS "Department wise Total
Salary" FROM Employee
GROUP BY DeptId;
Employee Table
DeptId Department wise Total Salary
NULL NULL
1 48000
2 42000
4 18000
By: Faizan Irshad