SQL PRACTICE: SQL SERVER - Total Count of Records
Q1. How to find the total number of records (number of rows) in SQL?
N.B: I am using the following:
Database: MSSQL
SQL Dialect: T-SQL
I have a created an employee table as follows:
-- Create a SQL database
Create database SQL;
use sql;
-- Create an employee table
create table employee(
EmpId int,
Name varchar(20),
Salary int
);
-- Insert values to the employee table
insert into employee (EmpId, Name, Salary)
Values
(100, 'Genet', 40000),
(100, 'Genet', 40000),
(101, 'Mike', 80000),
(101, 'Mike', 80000),
(102, 'Teddy', 60000),
(102, 'Teddy', 60000),
(103, 'Mat', 100000);
Ans: I have a couple of options to find the total records of the given table: using count or without count function.
1. Count () Function
select count (*) as Total_Row_Count from employee;
2. Window function (Aka without count function)
with cte as(
select *,
ROW_NUMBER() over (order by empid) as row_num
from employee)
select max(row_num) as Total_Row_Count from cte;
Comments
Post a Comment