MySQL COUNT

The COUNT function in MySQL returns the number of rows that match a specified condition. It is commonly used to get the number of records in a table or a result set.

Examples with Tamil Kings

1. Counting All Records

SELECT COUNT(*) AS total_kings FROM tamil_kings_auto_increment;

Code Explanation: This query counts all records in the tamil_kings_auto_increment table and returns the result as 'total_kings'.

2. Counting Records with a Condition

SELECT COUNT(*) AS kings_after_1000 FROM tamil_kings_auto_increment
WHERE reign_period > '1000 CE';

Code Explanation: This query counts records where the reign_period is after '1000 CE'.

Best Practices

  • Use COUNT(*) to count all rows, including those with NULL values.
  • Use COUNT(column_name) to count only rows where the specified column is not NULL.

Key Takeaways

  • The COUNT function returns the number of rows in a result set.
  • It is useful for determining the size of a dataset or the number of matching records.