0% found this document useful (0 votes)
5 views

SQL Notes

sql

Uploaded by

divijareddy0502
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

SQL Notes

sql

Uploaded by

divijareddy0502
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

//select all the records from titles

select * from titles // * ----- all columns

operators used in where condition


> >= < <= != =
and or
between not between
in not in
is is not ( null)
like not like
===========================
aggregate functions,,, sum, min,max,avg,count

select sum(price), max(price),min(price),avg(price),count(*),count(price)


from titles

count(*) -------- count total number of rows


count(columname)-------- count number of values in the column ( exclude null)

emp
--------
empid, ename,desig, sal, depcode,dob,city

//dispaly total sal of all emp , dep wise


select depcode,sum(sal) from emp
group by depcode

//total salary of all testers in every dept


select depcode,sum(sal) from emp
where desig='tester'
group by depcode

// display only depts in hyd,balore , and depts total sal>20 laks


select depcode, sum(sal) from emp
where city in('hyd','bangalore')
group by depcode
having sum(sal)>2000000

select .................. from table


where condition
group by column
having condition
order by column

=======
join

select c1,c2,c3,c4..........
from table1
[left / right / full outer] join table2
on table1.col=table2.col

student
------------
id,name,phone,email,course

marks
----------
id, score

select name,score from student


join marks
on student.id=marks.id

===========
sub query
----------
an sql query inside another sql query

select * from titles


where price>(select avg(price) from titles)

the inner query is executed first and output of innerquery is used


as input for outer query

inner query should select only single column

if inner query returns multiple values use "in" operator

=========================

what is primary key, foreign key, unique key


the column which is created primary key
. will not accept null
. will not accept duplicates
only one primary key is allowed to create on a table

Foreign key is the column which will refer to another


column(primary/unique)
the foreign key column will accept only values available in referred
column

uniqe key-- column will not accept duplicates ( 1 null value is allowed)
can have multiple unique keys on a table.

ex:
emp ( empid, ename,sal, desig, depcode)------
empid is primarykey,
depcode is foreignkey refer to depcode in dept table

dept (depcode, dname,manager) -----depcode is primarykey

what is diff delete, truncate,drop


delete
will delete records in a table
where condition can be used
truncate
will also delete records in table
cannot use where condition ( all records will be deleted)
drop
to remove the table also (records and table...etc)

ex:
delete emp where empid='5674'
truncate table emp
drop table emp

what is diff unique key, primary key

what is procedure, trigger

what is count(*), count(column)

what is diff union, union all


union--- will not display duplicates
union all------ will display duplicates

what is diff where, having

what is null in db

................... sql query examples..... select, group by, having, joins,


subquery
========================================

select * from titles

//select only title,type, price columns from titles


select title,type,price from titles

//where used to restrict records in select statement


select title,type,price from titles
where price>20

select * from employee


where sal>50000

select * from titles


where type='business'

select * from titles


where price>=15 and price<=20

select * from titles


where price between 15 and 20

select * from employee


where sal between 20000 and 50000

select * from titles


where price<15 or price>20

select * from titles


where price not between 15 and 20
select * from titles
where type='business' or type='psychology' or type='trad_cook'

select * from titles


where type in('business','psychology','trad_cook')

select * from titles


where type not in('business','psychology','trad_cook')

select * from employee


where city='hyd' or city='bangalore' or city='chennai'

select * from titles


where price is null

select * from titles


where price is not null

//all emp who don;t have phone but has emilid


select * from employee
where phone is null and email is not null

//get all managers in hyd


select * from employee
where city='hyd' and desig='manager'

//all managers in hyd and bangalore


select * from employee
where city in('hyd','bangalore') and desig='manager'

select * from employee


where (city='hyd' or city='bangalore') and desig='manager'

select * from titles


where title like 's%'

select * from titles


where title not like 's%'

select sum(price), max(price),min(price),avg(price),count(*),count(price)


from titles

select type,count(title_id)'No of Books' from titles


group by type
having count(title_id)>3

select type,sum(price) from titles


where pub_id='0877'
group by type
select * from titles
order by price desc

select * from titles


select * from publishers

select title,price,pub_name
from titles t
join publishers p
on t.pub_id=p.pub_id

select * from titles


select * from publishers
select * from titleauthor
select * from authors

select title,price,pub_name,au_lname+au_fname
from titles t
join publishers p
on t.pub_id=p.pub_id
join titleauthor ta
on ta.title_id=t.title_id
join authors a
on a.au_id=ta.au_id

select * from titles


where price>(select avg(price) from titles)

select * from publishers


where pub_id not in(select distinct pub_id from titles)

//emp who are doing more than 1 project


select * from emp
where eid in(select eid from projects
group by eid
having count(eid)>1)

//publishers who published more than 5 books


select pub_name from publishers
where pub_id in(select pub_id from titles
group by pub_id
having count(pub_id)>5)
========================================

You might also like