SQL COUNT

The COUNT function in SQL is used to count the number of rows in a table that meet a specified condition. This function is often used to find the total number of records in a table, or to determine the number of records that meet a certain criteria.

Syntax for COUNT

1SELECT COUNT(*) 2FROM table_name;

The * in the above syntax represents all columns in the table. If you only want to count the number of rows that meet a certain condition, you can specify the condition in the WHERE clause, like this:

1SELECT COUNT(*) 2FROM table_name 3WHERE column_name operator value;

For example, consider a table named employees with the following columns: id, first_name, last_name, salary. If you want to find the total number of employees in the table, you could use the following query:

1SELECT COUNT(*) 2FROM employees;

This query would return the number of rows in the employees table.

If you wanted to find the number of employees with a salary greater than $50,000, you could use the following query:

1SELECT COUNT(*) 2FROM employees 3WHERE salary > 50000;