4tested Codes
4tested Codes
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'
to add column salary to the customer table use the following code
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
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 minimum salary of the customer use the followimg code
selectmin(salary)from CUSTOMER
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)
selectROUND(sin(30),3)
Abs
The Abs function returns the absolute value of its parameter
Select ABS(-200)
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)
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)
(selectdistinct city
from employee)
union
(selectdistinct city
from company)