Select Count in SQL
? SELECT COUNT() in SQL
The COUNT() function returns the number of rows that match a specified condition or total rows in a table.
? Syntax:
SELECT COUNT(column_name) FROM table_name WHERE condition;or to count all rows including those with NULL in a column:
SELECT COUNT(*) FROM table_name WHERE condition;? Examples:
Count total rows in a table:
SELECT COUNT(*) AS total_employees FROM employees;Count rows where salary is not NULL:
SELECT COUNT(salary) AS salary_count FROM employees;Count employees in the 'Sales' department:
SELECT COUNT(*) AS sales_countFROM employeesWHERE department = 'Sales';Count distinct departments:
SELECT COUNT(DISTINCT department) AS unique_departments FROM employees;? Notes:
COUNT(*)counts all rows regardless of NULLs.COUNT(column_name)counts non-NULL values in that column.COUNT(DISTINCT column_name)counts unique non-NULL values.
Want me to show COUNT() with GROUP BY or joins?