Multiple Tables Row Count in Single Query in SQL Server

The following SQL Server query can be used to view multiple tables’ row counts in one single SELECT statement.

CREATE TABLE Dept(
EmployeeDetails VARCHAR(100));

INSERT INTO Dept VALUES
(‘John|Sales Team’),
(‘Mike|Sales’),
(‘Smith|Development’),
(‘Jim|New Sales Team’),
(‘Lucy|Development’),
(‘Will|Development’);

CREATE TABLE tbStudents(iStudentID INT, StudentName VARCHAR(10), SubjectName VARCHAR(30));
INSERT INTO tbStudents VALUES
(1, ‘John’, ‘Mathematics’),
(1, ‘John’, ‘Science’),
(1, ‘John’, ‘Chemistry’),
(2, ‘Smith’, ‘Economics’),
(2, ‘Smith’, ‘Mathematics’),
(2, ‘Smith’, ‘Commerce’),
(3, ‘Will’, ‘History’),
(3, ‘Will’, ‘Civics’),
(3, ‘Will’, ‘Economics’);

SELECT
(SELECT COUNT(*) FROM Dept) AS DeptTableRowCount,
(SELECT COUNT(*) FROM tbStudents) AS StudentsTableRowCount;

Hope this helps.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s