In SQL, the SELECT statement is a fundamental tool for retrieving data from a database. When paired with the COUNT() function, it becomes even more powerful, enabling efficient data aggregation and analysis. This article provides a detailed explanation of SELECT and COUNT() in SQL, including syntax, examples, and practical use cases. Whether we’re filtering rows, counting non-NULL values, or analyzing distinct entries, this article will help us master these essential SQL techniques.
What is SELECT in SQL?
The SELECT statement is used to retrieve specific columns or rows from a table based on defined conditions. It serves as the foundation of most SQL queries, allowing users to extract and filter data for analysis. Using select statements we can also apply filters using different clauses like where, Group By, Having, etc.
Syntax
SELECT <COLUMN_NAME>
FROM <TABLE_NAME>
WHERE <CONDITIONS>;
What is COUNT() in SQL?
The COUNT() function is an aggregate function used to count the number of rows available in a table or the number of rows that match condition criteria. It can count:
- All rows, using COUNT(*).
- Rows matching a specific condition, using COUNT(column_name).
- Unique values in a column, using COUNT(DISTINCT column_name).
Syntax
SELECT COUNT(column_name)
FROM <TABLE_NAME>
WHERE <CONDITIONS>;
Examples of SELECT with COUNT
To demonstrate the use of SELECT with COUNT, we will work with a sample table named Employee.
Step 1: Create the Employee Table
CREATE TABLE Employee (
Emp_ID INT PRIMARY KEY,
Emp_Name VARCHAR(50),
Phone_No VARCHAR(15),
Email VARCHAR(100),
Salary INT
);
Step 2: Insert Data into the Employee Table
INSERT INTO Employee (Emp_ID, Emp_Name, Phone_No, Email, Salary) VALUES
(100, 'Ram', '000000', 'ram@gmail.com', 10000),
(101, 'Shyam', '11111111', 'shyam@gmail.com', 12000),
(102, 'Mohan', '22222', NULL, 10000),
(103, 'Sohan', NULL, 'sohan@gmail.com', 15000),
(104, 'Rakesh', '44444', NULL, 18000),
(105, 'Suresh', '55555', NULL, 16000),
(106, 'Mukesh', NULL, 'mukesh@gmail.com', 17000),
(107, 'Mina', '77777', NULL, 20000);
Step 3: View the Employee Table
SELECT * FROM Employee;
Output
Emp_id | Emp_Name | Phone_no | Email | Salary |
|---|
100 | Ram | 000000 | ram@gmail,co | 10000 |
101 | Shyam | 11111111 | shyam@gmail.com | 12000 |
102 | Mohan | 22222 | - | 10000 |
103 | Sohan | - | sohan@gmail.com | 15000 |
104 | Rakesh | 44444 | - | 18000 |
105 | Suresh | 55555 | - | 16000 |
106 | Mukesh | - | mulesk@gmail.com | 17000 |
107 | mina | 77777 | - | 20000 |
Example 1: Count Total Rows in a Table
In this example, we apply the count function on the Emp_id column to count all rows in the Employee table which returns the number of rows that are not null
Query:
SELECT COUNT(Emp_id) FROM EMPLOYEE
Output
Example 2: Use, of AS keyword with Count
In this example, the AS keyword is used to assign a custom alias, Number_of_Employee, to the output column of the COUNT() function. This improves the readability of the result by giving a meaningful header name to the count of employee IDs.
Query:
SELECT COUNT (Emp_id) AS Number_of_Employee FROM EMPLOYEE
Output
Example 3: Count Non-NULL Values in a Column
In this example, the COUNT() function is applied to the Phone_No column to count rows that are not NULL. This helps determine how many employees have a phone number recorded in the database.
Query:
SELECT COUNT(Phone) AS Number_Available FROM EMPLOYEE
Output
Example 4: Count Rows Based on a Condition
In this example, we will count the number of employees whose salary is more than and equal to 15000
Query:
SELECT COUNT(Salary) AS SALARY
FROM EMPLOYEE
WHERE SALARY >=15000
Output
Example 5: Count Rows Using a Subquery
In this example, a subquery is used within the WHERE clause to calculate the average salary using the AVG() function. The main query then counts the number of employees whose salary is greater than this average, using the COUNT(*) function
Query:
SELECT COUNT(*) AS Above_Average_Salary
FROM Employee
WHERE Salary > (SELECT AVG(Salary) FROM Employee);
Output
Example 6: Count with Distinct
In this example, the DISTINCT keyword ensures that only unique (non-duplicate) values in the Salary column are counted. This is useful for identifying the number of distinct salary amounts in the dataset.
Query:
SELECT COUNT(DISTINCT Salary) AS Distinct_Salary FROM EMPLOYEE;
Output
Conclusion
The SELECT statement in SQL is a flexible and essential tool for retrieving and manipulating data from the database. When we use the COUNT() function with Select, it becomes more powerful, allowing us to perform data analysis and obtain valuable required data. The COUNT() function can also handle null values while counting rows and finding distinct values from the column. Mastering these functions will significantly improve our ability to work with databases and perform complex data analysis.
FAQs
Can I use SELECT in COUNT in SQL?
Yes, you can use SELECT with COUNT to count rows or specific conditions. For example:
SELECT COUNT(*) FROM table_name WHERE condition;
What is SELECT COUNT(*) in SQL?
SELECT COUNT(*) returns the total number of rows in a table, including rows with NULL values in any column. It is commonly used to determine the size of a dataset.
How do I select character count in SQL?
To count characters in a string column, use the LENGTH() function:
SELECT LENGTH(column_name) AS char_count FROM table_name;
Similar Reads
SQL SELECT COUNT()
In SQL, the SELECT statement is a fundamental tool for retrieving data from a database. When paired with the COUNT() function, it becomes even more powerful, enabling efficient data aggregation and analysis. This article provides a detailed explanation of SELECT and COUNT() in SQL, including syntax,
4 min read
SQL SELECT Query
The select query in SQL is one of the most commonly used SQL commands to retrieve data from a database. With the select command in SQL, users can access data and retrieve specific records based on various conditions, making it an essential tool for managing and analyzing data. In this article, weâll
4 min read
SQL Count() Function
In the world of SQL, data analysis often requires us to get counts of rows or unique values. The COUNT() function is a powerful tool that helps us perform this task. Whether we are counting all rows in a table, counting rows based on a specific condition, or even counting unique values, the COUNT()
7 min read
SQL - SELECT SUM
In SQL, the SELECT SUM statement is used to calculate the total sum of values in a numeric column. This powerful aggregate function allows you to summarize large sets of data, such as finding total revenue, product quantities, or any other numeric values in your database. In this article, we will ex
5 min read
SQLite COUNT
SQLite is a serverless database engine, it is written in C programming language and is easy to use and store the data. It is one of the most used database engines. It is widely used for the development of Embedded applications. In this article we will learn about the SQLite count, how it works and w
6 min read
COUNT() Function in MySQL
The COUNT() function in MySQL is a versatile aggregate function used to determine the number of rows or non-NULL values that match a specific condition in a query. It can be applied to an entire table or a particular column and is widely used in database operations to analyze the volume of data in a
3 min read
COUNT() Function in SQL Server
The COUNT() function in SQL Server is a fundamental aggregate function used to determine the number of rows that match a specific condition. Counting rows provides valuable insights into data sets such as the total number of records, distinct values, or records meeting certain criteria. In this arti
3 min read
MySQL LENGTH() Function
MySQL LENGTH() function returns the length of a string in bytes. SyntaxThe MySQL LENGTH function syntax is: LENGTH(string) ParametersThe LENGTH function accepts only one parameter. string: A specified string whose length is to be counted.MySQL LENGTH() Function ExamplesLet's look at examples of the
1 min read
SQL Clauses
Structured Query Language (SQL) is a powerful language used to manage and manipulate relational databases. One of the essential features of SQL is its clauses, which allow users to filter, sort, group, and limit data efficiently. SQL clauses simplify querying and enhance database performance by retr
7 min read
Count(*) vs Count(1) in SQL Server
In SQL Server, the COUNT() function is used to return the number of rows in a query result. There are different ways to use the COUNT() function such as COUNT(*) and COUNT(1). Although they produce the same result, there are subtle differences in how they work internally. In this article, we will tr
4 min read