In this article, we are going to write an SQL query to count the number of rows in a table. For this, we will be making use of the count() function of SQL. For this article, we will be making use of the Microsoft SQL Server as our database. Learning to count the number of rows in the table will be best if we learn it by seeing through a table so let’s deep dive into the table.
Let’s do the same by building a table inside the database and counting its rows. We will first create a database called “students”.
Creating a Database:
Use the below SQL statement to create a database called students.
CREATE DATABASE students;
We have the following student table in our student database :
-- Create a table called 'students'
CREATE TABLE students (
id INT PRIMARY KEY,
-- Insert some sample data into the 'customers' table
INSERT INTO students (id, name, email, phone)
(1, 'Aman Chopra', 'Aman@example.com', '123-456-7890'),
(2, 'Aditya Arpan', 'Aditya@example.com', '987-654-3210'),
(3, 'Shubham Thakur', 'Shubham@example.com', '555-555-5555'),
(4, 'Naveen tulasi', 'firstname.lastname@example.org', '987-654-3210'),
(5, 'Varsha Choudhary', 'email@example.com', '787-957-3657');
SQL Query To Count Number of Rows
The SQL COUNT( ) function is used to return the number of rows in a table. It is used with the Select( ) statement.
SELECT COUNT(column_name) from table_name;
Example: Using ‘ * ‘ we get all the rows as shown below.
SELECT * FROM students;
SELECT COUNT(id) from students;
We can even change the display name for displaying count:
Count Row with Using AS
SELECT COUNT(id) as id_count FROM students;
SQL Query To Count the Rows with HAVING Clause
We can use the HAVING clause in the SQL query to specify a condition for the COUNT function and also we can modify the code to only show the results for num_rows where the count is greater than 1.
SELECT phone, COUNT(*) as num_rows
GROUP BY phone
HAVING num_rows > 1;
SQL Query To Count the Rows with Order By Clause
The ORDER BY clause is used to sort the results of a SQL query by one or more columns. When used in conjunction with the COUNT() function, the ORDER BY clause can be used to sort the results by the count of a particular column.
For example, let’s say we have a student’s table with columns id, name, email, and phone. We want to count the number of students in each phone and then sort the results in descending order by the count of students in each phone. We can use the following SQL query:
SELECT phone, COUNT(*) as num_students
GROUP BY phone
ORDER BY num_students ASC;
In conclusion, the ORDER BY clause is used in SQL to sort the results of a query by one or more columns. When used with the COUNT() function, the ORDER BY clause can be used to sort the results by the count of a particular column. This allows us to sort the results of a query in ascending or descending order by the number of occurrences of a particular column value. By combining GROUP BY, COUNT(), and ORDER BY clauses, we can retrieve and sort summarized data in a meaningful way.
Unlock the Power of Placement Preparation!
Feeling lost in OS, DBMS, CN, SQL, and DSA chaos? Our Complete Interview Preparation
Course is the ultimate guide to conquer placements. Trusted by over 100,000+ geeks, this course is your roadmap to interview triumph.
Ready to dive in? Explore our Free Demo Content and join our Complete Interview Preparation