Documente Academic
Documente Profesional
Documente Cultură
2018-2019
SUBMITTED TO-
MR. AMIT
SRIVASTAVA
BASIC QUERIES
Creating Table
USE NPGCBCA3
AND OPERATOR :-
OR OPERATOR :-
ORDER BY :-
SELECT TOP:-
MIN OPERATOR :-
COUNT OPERATOR:-
AVG OPERATOR :-
SUM OPERATOR :-
LIKE OPERATOR:-
BETWEEN OPERATOR :-
NOT BETWEEN :-
IN OPERATOR : -
--Creating Database
use bca3
--Queries
Order by query
Group by Query-
into query-
having query-
select emp_name
from employee
where emp_name like '_a%';
query for in-
select city
from company
where city in (select city from employee);
alias query-
select emp_name
from employee as e;
exists query-
select company_name
from company
where exists
(select emp_name from works where salary=55000);
query for cartesian product-
VIEWS
--Creating Database
use Question_1
--Queries
calculation in select
Difference
select DIFFERENCE('Juicy','Jucy');
LTrim
QuoteName
SELECT QUOTENAME('abcdef','"');
replace function
Replicate
Select REPLICATE(name,2)
from employee;
Reverse
Select REVERSE(name)
from employee;
Soundex Value
STUFF
FUNCTIONS CREATION
Function along with If Else
return @ta;
end
--function calling
if else
if((select dbo.Prime(13))=1)
print 'Prime';
else
print 'Not Prime';
STORED PRODECURES
use Question_1;
Insert
Executing
SELECT
Executing
UPDATE
Executing
DELETE
create procedure Delete_rec
@emp_no varchar(10)
as
begin
DELETE FROM Employee
where emp_no=@emp_no;
end;
TRIGGERS
INSERT TRIGGERS
DELETE TRIGGER
UPDATE
UPDATE employee
set name='Raghav'
where emp_no=123;
View Triggers
Instead of Triggers
CREATE TABLE Student
(Id int Primary Key,
Name nvarchar(30),
Gender nvarchar(10),
SectionId int)
if(@SecId is null)
Begin
Raiserror('Invalid Section Name. Statement Terminated OK', 16, 1)
return
End
Instead of Update
Update ViewEmployeeDetails1
set Name = 'Satya1', DeptName = 'Blog'
where Id = 1
Update ViewEmployeeDetails1
set DeptName = 'Blog'
where Id = 1
if(Update(DeptName))
Begin
Declare @DeptId int
if(@DeptId is NULL )
Begin
Raiserror('Invalid Department Name', 16, 1)
Return
End
if(Update(Gender))
Begin
Update tblEmployee1 set Gender = inserted.Gender
from inserted
join tblEmployee1
on tblEmployee1.Id = inserted.id
End
if(Update(Name))
Begin
Update tblEmployee1 set Name = inserted.Name
from inserted
join tblEmployee1
on tblEmployee1.Id = inserted.id
End
End
Update ViewEmployeeDetails1
set Name = 'Satya11', Gender = 'Female', DeptName = 'Blog'
where Id = 1
Update ViewEmployeeDetails1
set Name = 'Satya11', Gender = 'Female', DeptName = 'BlogN'
where Id = 1
Update ViewEmployeeDetails1
set Name = 'Satya11', Gender = 'Female', DeptName = 'BlogN' , id=11
where Id = 1
Instead of Delete
create table orders (ord_no int, purch_amt float, ord_date varchar(11), c_id
varchar(30), s_id varchar(30))
Normal Join
--Prepare a list with salesman name, customer name, and their cities for the
salesman and customer who belong to the same city
Inner Join
--prepapre a list for whicj salesman are working for which customer along
with city and commissions earned by the salesman
-- Display the details of an order i.e. order number, order date, amount of
order, customer and salesman name and commission of the salesman for an order
-- Make a report with customer name, city, order no, date, amount, salesman
name and commission to find either any of the existing custoers have placed
no order or placed one or more orders by their salesmanor by own
-- Display the list of salesman who works eother for one or more customers or
not yet join under any of the customer wo placed either one or more orders or
no orderto theiir suppier
Cross Join
-- Make a cartesian product between salesaman and customer i.e. each slaeman
will appear for all customer and vice versa for that customer who belongs to
a city
select * from salesman a cross join customer where a.city is not null
-- make a report with customer name, city, order no, purchasae amount for
only those customers on the list who must gave a grade and placed one or more
orders or which order(s) have been placed ny the customer who is niether in
the list nor have a grade
-- make a list for the salesman who either wotk for one or more customers
oryet to join any of the customer. The customer may have placed, either one
or more orders on or above amount 2000 and must have a grade, or he may not
have placed any order to the associated supplier
WHile Loop
do while loop
DECLARE @X INT;
set @x=1;
WAY:
PRINT @X;
SET @X= @x + 1;
IF @X<=10 GOTO WAY;
DECLARE @X INT;
set @x = 11
WAY:
PRINT @X;
SET @X = @x +1;
IF NOT(@X >20) GOTO WAY;
QUESTIONS
Creating database –
create database assignment
use assignment
Write a query to display the name (first name and last name) for those
employees who gets more salary than the employee whose ID is 163.
Write a query to display the name ( first name and last name ),
salary, department id, job id for those employees who works in
the same designation as the employee works whose id is 169.
Write a query to display the name ( first name and last name ),
salary, department id for those employees who earn such amount of
salary which is the smallest salary of any of the departments.
SELECT *
FROM employees
WHERE employee_id IN (107,105,100);
Write a query to display all the information of the employees whose
salary is within the range 1000 and 3000.
SELECT *
FROM employees
WHERE salary BETWEEN
(SELECT MIN(salary)
FROM employees) AND 2500;
Write a query to display all the information for those employees whose
id is any id who earn the second highest salary.
SELECT *
FROM employees
WHERE employee_id IN
(SELECT employee_id
FROM employees
WHERE salary =
(SELECT MAX(salary)
FROM employees
WHERE salary <
(SELECT MAX(salary)
FROM employees)));
Write a query to display the employee number and name( first name and
last name ) for all employees who work in a department with any
employee whose name contains a T.
Write a query to display the employee number, name( first name and last
name ), and salary for all employees who earn more than the average
salary and who work in a department with any employee with a J in their
name.
Write a query to display the employee number, name( first name and last
name ) and job title for all employees whose salary is smaller than any
salary of those employees whose job title is MK_MAN. Exclude Job title
MK_MAN
Write a query to display the employee number, name( first name and last
name ) and job title for all employees whose salary is more than any
average salary of any department.
Write a query to display the department id and the total salary for
those departments which contains at least one employee.
Write a query to display the employee id, name ( first name and last
name ) and the job id column with a modified title SALESMAN for those
employees whose job title is ST_MAN and DEVELOPER for whose job title
is IT_PROG.
Write a query to display the employee id, name ( first name and last
name ), salary and the SalaryStatus column with a title HIGH and LOW
respectively for those employees whose salary is more than and less
than the average salary of all employees.
queries-
(find name & city of residence of all employees who work for sbi.)
update employee
set city = 'pune',street='meenar street'
where emp_name='hari';
queries-