Query Flashcards
query to create ,use and delete a database?
create database happy;
use happy;
drop database happy;
create and add values to a table?
create table employee(
e_id int not null,
e_name varchar(20),
primary key(e_id)
)
insert into employee values(
1,’name’)
select one column,multiple column,every column,distinct values in a column?
select x from employee;
select x,y from employee;
select * from employee;
select distinct x from employee;
where clause?
select * from employee where x=’female’;
AND ,OR, NOT operators?
select * from employee where cond1 AND cond2;
select * from employee where cond1 OR cond2;
select * from employee where NOT cond1;
LIKE and BETWEEN operators?
select * from employee where x LIKE ‘j%’;
select * from employee where x LIKE ‘3_’;
select * from employee where x BETWEEN 12 and 28;
query for different functions in sql?
min()
max()
count()
sum()
avg()
select min(x) from employee;
select * from employee where x=’male’;
query for different string functions?
LTRIM()
LOWER()
UPPER()
REVERSE()
SUBSTRING()
select LTRIM(‘ hello’);
select UPPER(‘hello’);
select SUBSTRING(‘this is sparta’,9,6);
ORDER BY and TOP?
select * from employee order by e_salary DESC;
select TOP 3 * from employee order by e_age DESC;
GROUP BY and HAVING clause?
select avg(e_salary),e_gender from employee GROUP BY e_gender;
select avg(e_age),e_dept from employee GROUP BY e_dept ORDER BY avg(e_age) DESC;
select e_dept,avg(e_salary) as avg_salary from employee GROUP BY
e_dept HAVING avg(e_salary)>100000 order by avg(e_salary) DESC;
update,delete and truncate statement?
update employee set e_age=42 where e_name=’sam’ ;
delete from employee where e_age=42;
truncate table employee;
different joints in SQL?
select employee.e_name,employee.e_dept ,department.d_name,department.d_location
FROM employee INNER JOIN department
ON employee.dept=department.d_name;
select employee.e_name,employee.e_dept ,department.d_name,department.d_location
FROM employee LEFT JOIN department
ON employee.dept=department.d_name;
select employee.e_name,employee.e_dept ,department.d_name,department.d_location
FROM employee RIGHT JOIN department
ON employee.dept=department.d_name;
select employee.e_name,employee.e_dept ,department.d_name,department.d_location
FROM employee FULL JOIN department
ON employee.dept=department.d_name;
update using JOIN?
update employee;
set e_age=e_age+10
FROM employee JOIN department
ON employee.dept=department.d_name
WHERE d_location=’newyork’
delete using JOIN?
delete employee
FROM employee JOIN department
ON employee.dept=department.d_name
where d_location=’newyork’
Union,except,intesect operators?
union
select * from x
union
select * from y
union all
select * from x
union all
select * from y
except
select * from x
except
select * from y
intersect
select * from x
intersect
select * from y
view in sql?
it is a subset table upon we can work
create view female_employee as
select * from employee
where e_gender=’female’;
drop view female_employee;
Alter in sql?
add,delete,modify columns in a table
alter table employee;
add e_dob date;
alter table employee;
drop column e_dob;
Merge in sql?
merge joins two tables source and target based on a common column
used defined function in sql?
scalar function-returns an scalar value
table valued function-returns a table
scalar function
CREATE FUNCTION function_name(@parm1 data_type,@parm2 data_type)
RETURNS return_datatype
AS
BEGIN
(function body)
RETURN value
END
table valued function
CREATE FUNCTION function_name(@parm1 datatype,@parm2 datatype)
RETURNS table
AS
RETURN (select columnlist from table where cond)
select * from dbo.function_name()
what is temporary table?
used to create temporary table and they are deleted as soon as the session is terminated,used to store and access intermediate data
CREATE TABLE #student(
s_id int,
s_name varchar(20)
);
insert into #student values(
)