How To Display 1 To 100 Numbers With Query?: Cte 1 Number Number 1 Cte Number 100 Cte

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

How to display 1 to 100 Numbers with query?

with cte as
(
select 1 number
union all
select number + 1 from cte where number < 100
)
select * from cte

How to remove duplicate rows from table?

delete from employee where id not in


(
select max( id) as maxrecord
from employee
group by firstName, LastName, Country)

Write an SQL query to find the position of the alphabet (‘a’) in the first name column
‘Amitabh’ from the Worker table.

SELECT CHARINDEX ('A','AMITABH') as 'CharacterPosition'

What is a Trigger in SQL Server?

alter trigger tr_insertEmployee2

on employee
AFTER insert
as
begin
declare @id int
select @id = Id from inserted
insert into Employee_Audit_Test
values('New Employee with ID ' + CAST(@ID as Varchar(10)) + ' is added at ' +
CAST(GETDATE() AS VARCHAR (50)))
end

How to select random record form a table?

SELECT TOP 1 first_name


FROM employees
ORDER BY NEWID();

select * from employees


Query to find Second Highest Salary of Employee?

select first_name, salary from employees e1


where 2 = (select count(distinct salary) from employees e2
where e1.salary <= e2.salary)

--using dense rank function

select top 1 employee_id,first_name,Salary from (


select employee_id,first_name,Salary, DENSE_RANK() over(order by salary desc) as R
from Employees) result
where result.R=1

Query to find duplicate rows in table?

SELECT
salary
FROM employees
GROUP BY salary
HAVING COUNT(salary) >1;

What is Query to display last 5 Records from Employee table?

SELECT TOP 5 * FROM employees


order by employee_id desc ;

What is Query to display Nth Record from Employee table?

SELECT * FROM (
SELECT
ROW_NUMBER() OVER (ORDER BY employee_id asc) AS rownumber,
employee_id
FROM employees
) AS foo
WHERE rownumber = 5

How to get 3 Highest salaries records from Employee table?

SELECT TOP 3 employee_id, first_name, salary FROM employees


order by salary desc ;
How to Display Odd rows in Employee table?

SELECT * FROM (
SELECT
ROW_NUMBER() OVER (ORDER BY employee_id asc) AS rownumber,
employee_id
FROM employees
) AS foo
WHERE rownumber %2 = 1

How to Display Odd rows in Employee table?

SELECT * FROM (
SELECT
ROW_NUMBER() OVER (ORDER BY employee_id asc) AS rownumber,
employee_id
FROM employees
) AS foo
WHERE rownumber %2 = 0

How Can i create table with same structure of Employee table?

SELECT *
into employeesemp
FROM employees

Display first 50% records from Employee table?

SELECT TOP 50 percent * FROM employees

or

SELECT
TOP 50 PERCENT Row_Number() over (order by employee_id) ,*
FROM
employees;

How to get distinct records from the table without using distinct keyword.

with cte ( dup_id, dup_name, dup_count)


as
(select dup_id, dup_name,
row_number () over ( partition by dup_id, dup_name order by dup_id ) as dup_count
from dup_table)
select * from cte where dup_count= 2 or 1

SQL query to find employees hired in last n months

Select *
FROM Employees1
Where DATEDIFF(MONTH, HireDate, GETDATE()) Between 1 and 1

How Do you find all Employees with its managers?

with emp_cte as
(select employeeid, employeename, managerid, 0 as emplevel
from tbEmployee where ManagerId is null
union all
select e.employeeid, e.employeename, e.managerid, emplevel + 1 as emplevel from
tbEmployee as e
inner join emp_cte as c on e.managerId= c.EmployeeId
)
select * from emp_cte

How to display following using query?

*
* *
* * *
* * * *

DECLARE @var int -- Declare


SELECT @var = 0 -- Initialization
WHILE @var < 5 -- condition
BEGIN -- Begin
PRINT replicate('* ', @var) -- Print
SET @var = @var + 1 -- decrement
END

You might also like