SELECT DepartmentID, Count(Salary) As Total, Min(Salary) AS MinSalary, Max(Salary) As MaxSalary, Avg(Salary) as AvgSalary, Sum(Salary) as SumSalary from Employee Group By DepartmentID;
SELECT Name,Salary,DepartmentID from Employee order by salary;
output:
1 2 3 4 5 6 7 8 9 10
+-------+--------+--------------+ | Name | Salary | DepartmentID | +-------+--------+--------------+ | Sam | 60000 | 1 | | Janet | 69000 | 1 | | Henry | 80000 | 2 | | Joe | 85000 | 1 | | Randy | 85000 | 1 | | Max | 90000 | 1 | +-------+--------+--------------+
ORDER BY ASC Multiply column
1 2 3 4
SELECT Name,Salary,DepartmentID from Employee order by salary, DepartmentID;
output
1 2 3 4 5 6 7 8 9 10
+-------+--------+--------------+ | Name | Salary | DepartmentID | +-------+--------+--------------+ | Sam | 60000 | 1 | | Janet | 69000 | 1 | | Henry | 80000 | 2 | | Joe | 85000 | 1 | | Randy | 85000 | 1 | | Max | 90000 | 1 | +-------+--------+--------------+
ORDER BY Desc
1 2 3
SELECT Name,Salary,DepartmentID from Employee order by salary DESC;
output
1 2 3 4 5 6 7 8 9 10
+-------+--------+--------------+ | Name | Salary | DepartmentID | +-------+--------+--------------+ | Max | 90000 | 1 | | Joe | 85000 | 1 | | Randy | 85000 | 1 | | Henry | 80000 | 2 | | Janet | 69000 | 1 | | Sam | 60000 | 1 | +-------+--------+--------------+
Join
Table: employee:
1 2 3 4 5 6 7 8 9 10
+----+-------+--------+--------------+ | id | Name | Salary | DepartmentID | +----+-------+--------+--------------+ | 1 | Joe | 85000 | 1 | | 2 | Henry | 80000 | 2 | | 3 | Sam | 60000 | 1 | | 4 | Max | 90000 | 1 | | 5 | Janet | 69000 | 1 | | 6 | Randy | 85000 | 1 | +----+-------+--------+--------------+
Table: department:
1 2 3 4 5
+----+------+ | ID | name | +----+------+ | 1 | IT | +----+------+
Inner Join
INNER JOIN: returns rows when there is a match in both tables.
1 2 3 4 5 6 7
SELECT Employee.Id as ID,Employee.Name as Name, Employee.salary as Salary, Employee.DepartmentID as DepartmentID, Department.Name as DepartmentName From Employee INNER JOIN Department on Employee.DepartmentId=Department.Id;
output:
1 2 3 4 5 6 7 8 9
+----+-------+--------+--------------+----------------+ | ID | Name | Salary | DepartmentID | DepartmentName | +----+-------+--------+--------------+----------------+ | 1 | Joe | 85000 | 1 | IT | | 3 | Sam | 60000 | 1 | IT | | 4 | Max | 90000 | 1 | IT | | 5 | Janet | 69000 | 1 | IT | | 6 | Randy | 85000 | 1 | IT | +----+-------+--------+--------------+----------------+
Left Join
LEFT JOIN: returns all rows from the left table, even if there are no matches in the right table.
1 2 3 4 5 6
SELECT Employee.Id as ID,Employee.Name as Name, Employee.salary as Salary, Employee.DepartmentID as DepartmentID, Department.Name as DepartmentName From Employee LEFT JOIN Department on Employee.DepartmentId=Department.Id;
output:
1 2 3 4 5 6 7 8 9 10
+----+-------+--------+--------------+----------------+ | ID | Name | Salary | DepartmentID | DepartmentName | +----+-------+--------+--------------+----------------+ | 1 | Joe | 85000 | 1 | IT | | 2 | Henry | 80000 | 2 | NULL | | 3 | Sam | 60000 | 1 | IT | | 4 | Max | 90000 | 1 | IT | | 5 | Janet | 69000 | 1 | IT | | 6 | Randy | 85000 | 1 | IT | +----+-------+--------+--------------+----------------+
Right Join
RIGHT JOIN: returns all rows from the right table, even if there are no matches in the left table.
1 2 3 4 5 6 7
SELECT Employee.Id as ID,Employee.Name as Name, Employee.salary as Salary, Employee.DepartmentID as DepartmentID, Department.Name as DepartmentName From Employee RIGHT JOIN Department on Employee.DepartmentId=Department.Id;
output:
1 2 3 4 5 6 7 8 9
+------+-------+--------+--------------+----------------+ | ID | Name | Salary | DepartmentID | DepartmentName | +------+-------+--------+--------------+----------------+ | 1 | Joe | 85000 | 1 | IT | | 3 | Sam | 60000 | 1 | IT | | 4 | Max | 90000 | 1 | IT | | 5 | Janet | 69000 | 1 | IT | | 6 | Randy | 85000 | 1 | IT | +------+-------+--------+--------------+----------------+