You are on page 1of 8

Tested codes by Yimenu

To create database and table


Create database db
Create table stu
(
[stuid] char(30) not null,
[Fname] char(30),
[Lname[ char(30),
)
To drop database and table
Drop database database name
Drop table table name

To drop column
altertable CUSTOMER
dropcolumn salary
To insert data to the databse
insertinto stu(Stuid,Fname,lname)
values ('Al-02','dessalegn','getie')

Insertinto stu(Stuid,Fname,lname)
values('al-02','daniel','asres')
Insertinto stu(Stuid,Fname,lname)
values('al-03','biruk','adinew')

update tables

update stu
set Fname='tadele'
where Fname='dessalegn'

ordering the data in ascending or descending


select*
from CUSTOMER
where fname>='dessalegn'
orderby fname desc

changing (altering) the table

to add column salary to the customer table use the following code

alter table CUSTOMER add salary char(30)

select stuid,Fname
from [bayech] .[dbo].[studentt]
where [Stuid]='al-03'and [Fname]='yetinayet'
to create table use the following codes
createtable occupation
(
[occupationcode] char(30)notnull,
[title] char(30)notnull,
[level] char(25)notnull,
[sector code] char(40)notnull

)
To insert data to the table use
the following codes
insertinto studentt(Stuid,Fname,Lname)
values('al-11','buzzuget','Abera')
Use the following codes to update table

update studentt
set Fname='genzeb'
where Fname='buzzuget'
to delete the record use the
following codes
delete studentt
where Fname='azenech'and Stuid='al-01'
the following code is used to retreive the
students whose fname >= F
select*
from [bayech].[dbo].[studentt]
where Fname>='F'
If u want to increase the salary of customer by 25%
update CUSTOMER
set salary=salary+salary*0.25
where age>=25

IN operator
SELECT fname from CUSTOMER where fname='yimen'or fname='zelalem'or
fname='abeba'

Is the same as

SELECT Fname FROM CUSTOMER WHERE


FnameIN(‘yimen’,’milion’)
Order by name
 The following code displays all the names
starts with letter D
SELECT fname from CUSTOMER where fname like'D%'
 to display names whose starts with D and ends
with n use the following code
SELECT fname from CUSTOMER where fname like'D%n'

Using functions
Aggregate Function in SQL
SQL allows grouping of resulting rows in a query so that aggregate functions can be applied to
make analysis and summary. The aggregate functions supported by the SQL statement are:
_ Summation
SUM (<column_name>)
Returns the sum of values for the numeric field (column) <column_name>.
_ Average
AVG (<column_name>)
Returns the average of values for the numeric field (column) <column_name>.
_ Minimum
MIN (<column_name>)
Returns the minimum of values for the numeric field (column) <column_name>.
_ Maximum
MAX (<column_name>)
Returns the maximum of values for the numeric field (column) <column_name>.
_ Count
COUNT (<column_name> | *)
Use paretheses for each function
To change all the names to the uppercase use the
following functions
select fname,UPPER(fname)from CUSTOMER
to find the squareroot of salary
selectsalary,sqrt(salary)from CUSTOMER
To find the average salary
selectAVG(salary)from CUSTOMER
to find the sum
selectsum(salary)from CUSTOMER
TO know how mony no of trainees
selectcount(fname)from CUSTOMER
to know the customers whose salary is >= 3500 use the
following code
selectcount(salary)from CUSTOMER where salary>=3500
to know the average salary whose salary is >= 3500 use the
following code
selectavg(salary)from CUSTOMER where salary>=3500

To find the maximum salary of customer above


3500
selectmax(salary)from CUSTOMER where salary>=3500

to find the minimum salary of the customer use the followimg code

selectmin(salary)from CUSTOMER

to display total no as list, average as avg and


maximum as max and minimum as min where salary is
>=2000
selectCOUNT(*)as list,
AVG(salary)as avg,
max(salary)as max,
MIN(salary)as min
from CUSTOMER where salary>=2000

more on functions
Sqrt
If you need to determine the square root of a value, you can use the Sqrt function.

Select SQRT(16)

Square
The Square function returns the square of a value. ie. the result of multiplying the value by itself.
Select SQUARE(10)

Power
The last function in this article is Power. This raises one value to the power of another. The first
parameter contains the value to be raised and the second parameter specifies the power to use.
Select POWER(2,12)

LOG10(expression)
Returns the logarithms of the number with the base 10
Select LOG10(100)

rounding of the decimal numbers

selectROUND(sin(30),3)

Abs
The Abs function returns the absolute value of its parameter

Select ABS(-200)

To create relation relation ship b/n tables


createtable saving
(
[accont number] char(30)not null primarykey,
[fname] char(20),
[lname] char(30),
[saving amount] numeric(20,4),
[deposite] numeric(30,4),
[Loan id]char(20)foreign key references loan
)
createtable loan
(
[Loan id] char(20)notnullprimarykey,
[Fname] char(20),
[Lname]char(30),
[deposite] numeric(30,4),
)

To delete record from the table worker


delete worker
where [worker id]='wor-09'
Example
createdatabase bank
createtable saving
(
[accont number] char(30)not null primarykey,
[fname] char(20),
[lname] char(30),
[saving amount] numeric(20,4),
[deposite] numeric(30,4),
[Loan id] char(20)foreign key references loan([Loan id])
)
Create table loan
(
[Loan id] char(20)notnullprimarykey,
[Fname] char(20),
[Lname]char(30),
[deposite] numeric(30,4),
)
createtable worker
(
[worker id] char(30)notnullprimarykey,
[fname] char(30),
[Lname] char(30),
[sex] char(10),
[salary] numeric(10,2)
)
insertinto saving([accont number],fname,lname,[saving amount],deposite,[Loan
id])
values('abay-034','biskut','tesfaye','5000.0','35000.0','lo-056')
insertinto saving([accont number],fname,lname,[saving amount],deposite,[Loan
id])
values('abay-034','biskut','tesfaye','5000.0','35000.0','lo-056')
insertinto saving([accont number],fname,lname,[saving amount],deposite,[Loan
id])
values('abay-035','sebilewongel','masresha','6000.0','34000.0','lo-055')
insertinto worker([worker id],fname,Lname,sex,salary)
values('wor-05','yirgedu','adefirs','F','4000')
insertinto worker([worker id],fname,Lname,sex,salary)
values('wor-08','ayinalem','tefera','F','3500')
insertinto worker([worker id],fname,Lname,sex,salary)
values('wor-011','ayele','asefa','M','3150')
insertinto worker([worker id],fname,Lname,sex,salary)
values('wor-013','fanas','yeshitila','F','2500')
insertinto worker([worker id],fname,Lname,sex,salary)
values('wor-09','alemitu','teferi','F','4500')
insertinto loan([Loan id],Fname,Lname,deposite)
values('lo-09','tsehay','abebe','32000')
insertinto loan([Loan id],Fname,Lname,deposite)
values('lo-012','etagegn','abebe','50000')
altertable worker
add [university] char(20)
altertable saving
add [phone number] numeric(20)
altertable loan
add [loan amount] numeric(20,4)

update worker
set university='Addis Ababa'
where [worker id]='wor-05'
update worker
set university='Bahir Dar'
where [worker id]='wor-09'
select*
from worker
where [worker id]='wor-05'
update worker
set salary=salary+0.4*salary
where sex='F'
select*
from worker
where fname like'y%'
select fname,
lower(fname)
from worker
selectSQRT(salary)from worker
selectAVG(salary)from worker
selectSUM(salary)from worker
selectCOUNT(fname)from worker
selectCOUNT(salary)from worker
where salary>=3500
selectAVG(salary)from worker
where salary>=4000
selectCOUNT(*)as list,
AVG(salary)asavg,
MAX(salary)asmax
from worker
where salary>=4500
droptable loan
droptable saving

nested query
A query inside another query is called nested query.
write the code to display the name of the customer who take loan
select l.Name,l.Lname
from LOAN l
where exists(select*
from CUSTOMER c
where l.Name=c.fname)

1.Find the names, cities of employees who work for


exactly one company.
select e.person_name. e.city
from employee e
where 1 = (select count(*)
from works w
where w.person_name = e.person_name)
Q6) Find the names of the employees who are not a
manager.
select person_name
from employee
wherenot exists (select *
from manages
where employee.person_name =
manages.manager_name)

Find the names of the employees who are not a


manager.
(select person_name
from employee)
except
(select manager_name
from manages)

or
select person_name
from employee
wherenot exists (select *
from manages
where employee.person_name =
manages.manager_name)
select person_name
from employee
where person_name not in (select manager_name
from manages)

Find all cities where employees live or where


companies are located.

(selectdistinct city
from employee)
union
(selectdistinct city
from company)

Find the streets of employees who work for all


companies in Boston.
selectdistinct e.street from employee e, works w
where e.person_name = w. person_name
and not exists ((select company_name from company
where city = “Boston”)
except
(select company_name
from works w1
where w1.person_name = w.person_name))

You might also like