SQL EXAMPLES

Query to Verify Duplicates Exist in Department table...

SELECT DepartmentName, COUNT(*)
FROM Department
GROUP BY DepartmentName
HAVING COUNT(*) > 1


Find the second largest data from the Categories table...

select max(CategoryId) from Categories where CategoryId < (select max(CategoryId) from Categories)
select max(FeeTotalAmount) from Fees where FeeTotalAmount not in (select max(FeeTotalAmount) from Fees)


Get the top 5 data from Categories table...

select * from Categories where CategoryId in (select distinct top 5 CategoryId from Categories order by CategoryId desc)
Without using keyword TOP :-      select * from  Categories order by CategoryId desc offset 0 rows fetch first 5 rows only


Update a table by converting all 'Male' to 'Female'...

update Users
     set Gender = case
                when Gender = 'Male' then 'Female'
                else 'Female'
                end