You are on page 1of 7

USE hr

GO

SELECT * FROM regions


GO
SELECT LAST_NAME,FIRST_NAME, SALARY
FROM employees
GO
-- Display The employee_id,last_name,department_name for all the employees
SELECT employee_id,last_name,department_name
FROM employees e LEFT JOIN departments d
ON(e.DEPARTMENT_ID=d.DEPARTMENT_ID)
-- Display The employee_id,last_name,department_name for all the departments
SELECT employee_id,last_name,department_name
FROM employees e RIGHT JOIN departments d
ON(e.DEPARTMENT_ID=d.DEPARTMENT_ID)
-- Display the Employee_id,last_name,Manager_id,Manager's last_name
SELECT e.EMPLOYEE_ID, e.LAST_NAME, e.MANAGER_ID,m.LAST_NAME AS "Manager's
last_name"
FROM employees e JOIN employees m ON(e.MANAGER_ID=m.EMPLOYEE_ID)
--- The HR department needs a report of all employees
--- with corresponding departments.
--- Write a query to display the last name, department number,
--- and department name for these employees.
SELECT e.LAST_NAME,e.DEPARTMENT_ID,d.DEPARTMENT_NAME
FROM employees e LEFT JOIN departments d
ON(e.DEPARTMENT_ID=d.DEPARTMENT_ID)
--- The HR department wants to determine the names and Salaries
-- of all employees who earned less than Greenberg.
--- Sort the output by salary in descending order.
SELECT e.LAST_NAME, e.SALARY
FROM employees e
WHERE e.SALARY<(SELECT SALARY FROM employees WHERE last_name='Greenberg')
ORDER BY e.SALARY DESC
-- Display the department_name, the number of employees in the department,
-- and the total salary, for all the departments
-- (include the department which has no employee).
-- Label the Columns as Department,Employees and Total.
SELECT d.DEPARTMENT_NAME,
COUNT(e.employee_id) AS "Employees",
SUM(e.SALARY) AS "Total"
FROM departments d LEFT JOIN employees e
ON(d.DEPARTMENT_ID=e.DEPARTMENT_ID)
GROUP BY d.DEPARTMENT_NAME
GO
SELECT @@SERVERNAME
GO
SELECT COLUMN_NAME,DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='EMPLOYEES'
GO
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
GO
--- DIsplay the last_name concatenated with the first_name with space
--- for the employees in the IT department.
--- Name the column Full Name +

SELECT last_name+' '+first_name AS "Full Name"


FROM employees e JOIN departments d
ON(e.DEPARTMENT_ID=d.DEPARTMENT_ID)
WHERE (d.DEPARTMENT_NAME='IT')
GO
-------------------------------------
----- Character functions -----------
-------------------------------------
- LEFT ---- Extract a number of characters from the left
SELECT LEFT('ABCDEFG',4) ---> 'ABCD'
- RIGHT ---- Extract a number of characters from the right
SELECT RIGHT('ABCDEFG',4) ---> 'DEFG'
- UPPER ,LOWER, LEN (returns the number of characters)
SELECT last_name,UPPER(last_name),LEN(last_name)
FROM employees WHERE department_id=60;
- CONCAT Contatenate
---- Display the last_name concatenate with first_name and JOB_ID
---- With space for the first 10 employees
SELECT TOP 10 CONCAT(last_name,' ',first_name,' ',JOB_ID)
FROM employees
--- SUBSTRING ------ Extract Characters
--- SUBSTRING(string,start,length)
--- 'ABCDEFG'
--- SUBSTRING('ABCDEFG',1,3) ----> 'ABC'
--- SUBSTRING('ABCDEFG',4,3) ----> 'DEF'
-- 54321
--- SUBSTRING('ABCDEFG',-5,3) ----> 'CDE'

- Write a query that displays the last name


- and the length of the last name for
- all employees whose name starts with the letters "J","A", or "M"
SELECT LAST_NAME,LEN(Last_name) AS "Length"
FROM employees
WHERE LEFT(last_name,1) IN ('J','A','M')
---Date functions
--------------
SELECT GETDATE() ------ Return the current date and time ---
2024-03-11 14:03:07.590
DAY ----- Returns the day
MONTH ----- Returns the Month
YEAR ----- Returns the YEAr
SELECT DATEADD(MONTH,2,GETDATE()) --- Returns the data after 2 Months
GO
SELECT DATEADD(DAY,2,GETDATE()) --- Returns the data after 2 Days
GO

DATEDIFF(Interval,value,date1,date2)

SELECT DAY(GETDATE()) "DAY",MONTH(GETDATE()) "MONTH",YEAR(GETDATE()) "YEAR"


DAY MONTH YEAR
----------- ----------- -----------
11 3 2024
SELECT DATENAME(WEEKDAY,GETDATE())
GO
---- Display the employees last_name,hire_date and the
--- number of year that the employees worked with the company
SELECT last_name,hire_date,DATEDIFF(YEAR,HIRE_DATE,GETDATE())
FROM employees
---- DATEPART
SELECT last_name,hire_date,DATEPART(MONTH,HIRE_DATE)
FROM employees
-----
SELECT DATENAME(MONTH,GETDATE())
GO
----------
-- Conversion functions
-----------
CONVERT(datatype,expression,style)

SELECT CONVERT(VARCHAR(19),GETDATE())
SELECT CONVERT(VARCHAR(19),GETDATE(),110) --- 03-11-2024
SELECT CONVERT(VARCHAR(19),GETDATE(),20) --- 2024-03-11 14:24:03
SELECT CONVERT(VARCHAR(19),GETDATE(),105) --- 11-03-2024
---------
SELECT CONVERT(VARCHAR(19),1234564)
---------
-- Create a query to display the total number of employees and, of that total,
-- the number of employees hired in 2001 .. 2008 .
-- Create appropriate column headings
--- Display the last_name, the year that the employees started.
TOTAL 2001 2002 2003 2004
---------- ---------- ---------- ---------- ----------
251 76 74 74 27

SELECT COUNT(*) total,


SUM(CASE WHEN DATEPART(YEAR,hire_date)=2001 THEN 1 ELSE 0 END) AS "2001",
SUM(CASE WHEN DATEPART(YEAR,hire_date)=2002 THEN 1 ELSE 0 END) AS "2002",
SUM(CASE WHEN DATEPART(YEAR,hire_date)=2003 THEN 1 ELSE 0 END) AS "2003",
SUM(CASE WHEN DATEPART(YEAR,hire_date)=2004 THEN 1 ELSE 0 END) AS "2004",
SUM(CASE WHEN DATEPART(YEAR,hire_date)=2005 THEN 1 ELSE 0 END) AS "2005",
SUM(CASE WHEN DATEPART(YEAR,hire_date)=2006 THEN 1 ELSE 0 END) AS "2006",
SUM(CASE WHEN DATEPART(YEAR,hire_date)=2007 THEN 1 ELSE 0 END) AS "2007",
SUM(CASE WHEN DATEPART(YEAR,hire_date)=2008 THEN 1 ELSE 0 END) AS "2008"
FROM employees;

SELECT last_name,DATEPART(YEAR,HIRE_DATE) AS "Year"


FROm employees

SELECT MIN(DATEPART(YEAR,HIRE_DATE)) AS "Min",


MAX(DATEPART(YEAR,HIRE_DATE)) AS "MAx"
FROm employees

---- Create a matrix query to display the job_id,


--- the salary for that job based on the department
---- number, and the total salary for that job,
---- for departments 20, 50, 80, and 90, giving each
--- column an appropriate heading.
JOB_ID DEPT 20 DEPT 50 DEPT 80 DEPT 90 Total
---------- -------- -------- -------- -------- --------
AD_VP 0 0 0 34,000 34,000
SH_CLERK 0 64,300 0 0 64,300
ST_CLERK 0 55,700 0 0 55,700
SA_MAN 0 0 61,000 0 61,000
ST_MAN 0 36,400 0 0 36,400
AD_PRES 0 0 0 24,000 24,000
MK_MAN 13,000 0 0 0 13,000
SA_REP 0 0 243,500 0 243,500
MK_REP 6,000 0 0 0 6,000
SELECT JOB_ID,
SUM(CASE WHEN DEPARTMENT_ID=20 THEN SALARY ELSE 0 END) "DEPT 20",
SUM(CASE WHEN DEPARTMENT_ID=50 THEN SALARY ELSE 0 END) "DEPT 50",
SUM(CASE WHEN DEPARTMENT_ID=80 THEN SALARY ELSE 0 END) "DEPT 80",
SUM(CASE WHEN DEPARTMENT_ID=90 THEN SALARY ELSE 0 END) "DEPT 90",
SUM(SALARY) "TOTAL"
FROM employees
WHERE DEPARTMENT_ID IN (20, 50, 80,90)
GROUP BY JOB_ID;

--- Transact SQL


--- Write an TSQL statement to display the following output
last_name started on name of the day and worked for year_worked years
for the employees whose last_name start with A, S or M
and joined the company between 2004 and 2007.

Smith started on Tuesday and worked for 20 years

SELECT LAST_NAME+' started on '+DATENAME(WEEKDAY,hire_Date)+' and worked for '+


CONVERT(VARCHAR(3),DATEDIFF(YEAR,HIRE_DATE,GETDATE()))+' years' "Employees Info"
FROM employees
WHERE DATEPART(YEAR,HIRE_DATE) BETWEEN 2004 AND 2007
AND LEFT(LAST_NAME,1) IN ('A','S','M')
GO
-------
CREATE TABLE TEST(ID INT PRIMARY KEY IDENTITY(1,1) ,
MARKS NUMERIC(5,2),
MODULE VARCHAR(25));
-------
INSERT INTO TEST(MARKS,MODULE) VALUES(90,'PLSQL');
SELECT * FROM TEST;
INSERT INTO TEST(MARKS,MODULE) VALUES
(90,'PLSQL'),(60,'SQL'),(80,'Windows');
-------------------------------------------
-- The variable name start with @
DECLARE @last_name VARCHAR(25)
-- Assign value to the variable
SET @last_name ='Ali'
-- Display on the screen
PRINT(@last_name)

Create a procedure dispemp with one parameter to


display the last_name and salary of the employees who
earn more than 65000.

CREATE OR ALTER PROCEDURE dispemp(@salary NUMERIC)


AS
SELECT last_name,salary FROM employees WHERE salary>@salary

EXECUTE dispemp 6500

Create a procedure to Display the Last_name and Salary of one employee_id.

Last Name : King


Salary : 24000

CREATE OR ALTER PROCEDURE empsal(@empid NUMERIC)


AS
DECLARE @lname VARCHAR(25),@sal NUMERIC(10);
SELECT @lname=last_name,@sal=salary
FROM employees WHERE employee_id=@empid;
PRINT('Last Name : '+@lname)
PRINT('Salary : '+CONVERT(VARCHAR(5),@sal))

EXECUTE empsal 178

Create OR ALTER function circle_area which returns the area of a circle with a
radius r.
CREATE OR ALTER FUNCTION circle_area(@radius float)
RETURNS float
AS
BEGIN
DECLARE @PI float=3.14,@AREA float;
SET @AREA=@RADIUS*@RADIUS*@PI;
RETURN @AREA;
END;

SELECT DBO.circle_area(10)

DECLARE @r FLOAT
SET @r=100
PRINT(DBO.circle_area(@r))
------
Write a TSQL Program which displays the first_name, last_name, Salary,city
and country of one employee_id.
Display the output as shown below .

eg. Employee ID=176

Employee ID : 176
First Name : Verney Last Name : Gravenell
Salary : 21000
City : New York Country : United States

CREATE OR ALTER FUNCTION RPAD(@text VARCHAR(25),@length INT)


RETURNS VARCHAR(100)
BEGIN
DECLARE @out VARCHAR(100);
RETURN @text+REPLICATE(' ',@length-len(@text));
END;

DECLARE @lname VARCHAR(20),@fname VARCHAR(20),@sal NUMERIC,@id NUMERIC;


DECLARE @city VARCHAR(20),@country VARCHAR(20);
SET @id=179
SELECT
@lname=e.last_name,@fname=e.first_name,@sal=e.salary,@city=l.city,@country=c.countr
y_name
FROM employees e
JOIN departments d ON(e.department_id=d.department_id)
JOIN locations l ON(l.location_id=d.location_id)
JOIN countries c ON(c.country_id=l.country_id)
WHERE e.employee_id=@id;
PRINT('Employee ID : '+CONVERT(VARCHAR(4),@ID))
PRINT('First Name : '+DBO.RPAD(@fname,25)+' Last Name : '+@lname)
PRINT('Salary : '+CONVERT(VARCHAR(5),@sal))
PRINT('City : '+DBO.RPAD(@city,25)+ ' Country : '+@country)

--- LOOP
while condition
BEGIN
TSQL_statement|BREAK;
END;

Display the times table of one integer <=10


DECLARE @input INTEGER,@counter INTEGER;
SET @input=7;
SET @counter=1;
WHILE @counter<=10
BEGIN
PRINT(str(@counter,2)+' x '+str(@input,2)+ ' = '+str(@counter*@input,2))
SET @counter=@counter+1
END;
--------- COnditional Statement
IF condition
BEGIN

END;
ELSE
BEGIN

END;

Display the total even and total odd for integer less than 11

DECLARE @even INT=0,@odd INT=0,@counter INT=0


WHILE @counter<=10
BEGIN
IF @counter % 2 = 0
BEGIN
SET @even= @even+@counter
END
ELSE
BEGIN
SET @odd= @odd+@counter
END
SET @counter=@counter+1
END
PRINT('Total odd : '+CONVERT(VARCHAR(5),@odd))
PRINT('Total even : '+CONVERT(VARCHAR(5),@even))
----------------------------
Write a TSQL program to display the division of two numbers.

DECLARE @first int=100,@second int=0,@result NUMERIC(10,4)=0;


BEGIN TRY
SET @result=@first/@second
PRINT(@result)
END TRY
BEGIN CATCH
IF @second=0
PRINT('Division By Zero')
END CATCH
------------------
Implicite cursor : SELECT returns 1 row, UPDATE,DELETE,INSERT
Explicite cursor : SELECT returns more than 1 row.
Youh ave to DECLARE the cursor
OPEN the cursor
FETCH the cursor
CLOSE the cursor
Display the last_name,First_name,Salary of the employees in One Department_Id.

DECLARE @lname VARCHAR(25),@fname VARCHAR(25),@sal NUMERIC,@id INT=60;


DECLARE dept_cur CURSOR FOR
(SELECT last_name,First_name,Salary FROM employees WHERE Department_Id=@id)
BEGIN
OPEN dept_cur
FETCH NEXT FROM dept_cur INTO @lname,@fname,@sal
while @@FETCH_STATUS = 0
BEGIN
PRINT(DBO.RPAD(@lname,20)+DBO.RPAD(@fname,20)+CONVERT(VARCHAR(10),@sal))
FETCH NEXT FROM dept_cur INTO @lname,@fname,@sal
END
CLOSE dept_cur
DEALLOCATE dept_cur
END

You might also like