GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — SQL Count() Function - GeeksforGeeks SQL Count() FunctionLast Updated : 22 Nov, 2025 The SQL COUNT() function is used to return the number of rows or non-null values in a column. It helps summarize data in reports and analytics.
Example: First, we create a demo SQL database and table, on which we will use the COUNT() functions. ![]() Query: SELECT COUNT(*) AS TotalEmployees FROM Employee; Output: ![]()
Syntax: COUNT(expression)1. Count all rows: SELECT COUNT(*) FROM table_name;2. Count distinct values in a column: SELECT COUNT(DISTINCT column_name) FROM table_name;Examples of SQL Count FunctionLet’s explore practical examples of the COUNT() function using a sample Customers table; consider the table below for all examples. ![]() 1. Counting the Number of Rows with COUNT(*)When we want to count all the rows in a table, regardless of the column values, we can use COUNT(*). It counts every row, including rows with NULL values. Query: SELECT COUNT(*) AS TotalCustomers Output: ![]()
2. Counting Unique Values with COUNT(DISTINCT …)Sometimes, we may need to count only the distinct values in a column. The COUNT(DISTINCT column_name) function allows us to count only unique entries in a column, ignoring duplicates. Query:SELECT COUNT(DISTINCT Country) FROM Customers;Output: ![]()
3. Count Rows That Match a Condition Using COUNT() with CASE WHENWe can use the COUNT() function along with CASE WHEN to count rows that match a specific condition. This is helpful when we want to count rows based on certain criteria without filtering the rows out of the result set. Query: SELECT COUNT(CASE WHEN Age > 30 THEN 1 ELSE NULL END) AS Adults Output: ![]()
4. Count Rows in Groups Using COUNT() with GROUP BYWe can use the COUNT() function with GROUP BY to count rows within groups based on a column. This is useful when we want to categorize data and then count how many records exist in each category. Query: SELECT Country, COUNT(*) AS CustomerCount Output: ![]()
5. Filter Groups Using COUNT() with GROUP BY and HAVINGWe can combine the COUNT() function with HAVING to filter the results after grouping. The HAVING clause is used to specify conditions on groups, similar to the WHERE clause, but for groups. Query:SELECT Country, COUNT(*) AS CustomerCount Output: ![]()
Best Practices for Using the COUNT() FunctionThe COUNT() function is powerful and widely used, but to keep queries efficient and easy to maintain, it's important to follow a few best practices. 1. Optimize Queries for Large Datasets:On large tables, COUNT() can be slow if the database must scan the entire dataset. Adding indexes to frequently counted columns improves performance. Query: -- Create index on Country column Output: ![]()
2. Avoid Complex COUNT Queries for Large Tables:Queries with many conditions or subqueries can be slow on very large tables. Simplify or break them into smaller parts to improve performance. Query: SELECT COUNT(*) Output: ![]()
Comment ExploreData Constraints & Aggregate Functions |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |