Learning SQL Command, SQL-Master.net

SQL command dictionary for beginners. Provide both command examples and results.

Aggregate Functions

COUNT function to count number of rows

Publish Date:

COUNT function is used to count a number of rows. If you want to count only unique elements, you can use DISTINCT to exclude duplicated elements.

Example 1: count a number of employees

SELECT COUNT(*) FROM tbEmpTable;

COUNT(*)
24

Example 2: count numbers of employees by each department.

SELECT deptCD, COUNT(*) FROM tbEmpTable GROUP BY deptCD;

deptCD COUNT(*)
100 3
200 10
300 7
400 4

Example 3: count a number of departments

SELECT COUNT(DISTINCT deptCD) FROM tbEmpTable;

COUNT(DISTINCT deptCD)
4

ad

ad

-Aggregate Functions

Related Articles

no image

AVG function to calculate an average value

AVG function is used to calculate an average value from a selected field. Example1: Calculate an average value of employees’ salary. SELECT AVG(salary) FROM tbSalaryTable; AVG(salary) 254000 Example2: Calculate an average value of salary by each department. SELECT deptCD, AVG(salary) FROM tbSalaryTable GROUP BY deptCD; deptCD AVG(salary) 100 150000 200 203090 300 403200 400 305420

ad