We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 6
‘Save table to another table
into file_name: save result in another table (BASE TABLE)
select distinct customerlastname into temp
from customer
order by customerlastname
select * from temp ~-see the table (data type will remain)
8. Like (search something) ~ (underscore sign) _is only specific for one character only
~ (percent sign) % represents zero, one, or multiple characters
select * from customer
where customerlastname like ' 1%"
‘9. In search something) ~ search multiple items
select * from customer
where customerlastname in
10. > (search something) select * from customer
where customerlastname > ‘Brown’ or customerlastname>'Cross’
11. = (Not Equal) select * from customer
where customerlastname <> ‘Brown’
12. 1S NULL = check null values
select * from customer
where customerlastname IS NULL
13. IS NOT NULL select * from customer
where customerlastname IS NOT NULL
14, between select * from sale
where saleunitprice between $ and 10 ~not include S & 10
15. count ~ returns the number of rows ina table
~ AS means aliasing, temporary giving name to a column/ table
select count(*) as [Number of Records] from customer
where customerfirstname like '8%'
16. sum select sale employeeid EmployeeFirstName, EmployeeLastName , count(") as
[Number of order}
sum(salequantity) as (Total Quantity)
from sale employee
where sale. employeeid = employee employeeid
group by sale employeeid EmployeeFirstName, EmployeeLastName
17. count month select month(saledate) as [Month], count ( * ) as [Number of sale],
sum(salequantity” saleunitprice) as [Total Amount]
from sale
group by month(saledate)
18. max SELECT MAX(Salary)
FROM EmployeeSalary
19. min SELECT MIN(Salary)
FROM EmployeeSalary
20. average SELECT AVG(Salary)
FROM EmployeeSalary1. Create database create database SaleOrder
2._ Use the database use SaleOrder
3. Create tables create table dbo customer (
CustomeriO int NOT null primary key,
CustomerfirstName varchar(50) NOT nul
CustomerLastName varchar(50) NOT nul
CustomerAddress varchar(50) NOT null
CustomerSuburb varchar(50) null
Customercity varchar(50) NOT null
CustomerPostCode char(4) null
CustomerPhoneNumber char(12) null
create table dbo.inventory (
InventoryIO tinyint NOT null primary key,
InventoryName varchar(50) NOT null
InventoryDescription varchar(255) null,
2
SELECT JobTitle, AvG(Salary)
FROM EmployeeDemographics ED
JOIN EmployeeSalary ES
ON ED.EmployeeID = ES. EmployeeID
GROUP BY JobTitle
HAVING AVG(Salary) > 4500
ORDER BY AVG(Salary)
22. Change data type ~ CAST(expression AS datatypellength))
temporary for use SELECT CAST('2017-@8-25 0@:00:00.000' AS date)
~ CONVERT(data_type(length), expression, style)
SELECT CONVERT(date, '2017-@8-25 0:00:00.000')
23. CASE Statement SELECT FirstName, LastName, Age,
CASE
WHEN Age > 30 THEN ‘Old*
WHEN Age BETWEEN 27 AND 38 THEN ‘Young’
ELSE ‘Baby’
END
| FROM EmployeeDemographics ED
WHERE Age IS NOT NULL
ORDER BY Age
SELECT FirstName, LastName, JobTitle, Salary,
CASE
WHEN JobTitle = ‘Salesman’ THEN Salary + (Salary *.1@)
WHEN JobTitle = ‘Accountant’ THEN Salary + (Salary *.@5)
WHEN JobTitle = 'HR' THEN Salary + (Salary *.0000e2
ELSE Salary + (Salary *.@3)
END AS SalaryAfterRaise
FROM EmployeeDemographics £D
)OIN EmployeeSalary ES
ON ED.EmployeeID = ES.EmployeeID
24, Partition By SELECT FirstName, LastName, Gender, Salary,
“returns a single value for each | COUNT(Gender) OVER (PARTITION BY Gender) AS TotalGender
row FROM EmployeeDemographics ED
JOIN EmployeeSalary ES
ON ED.EmployeeID = ES.EmployeeID
“Femme Lastame Ganser Saloy fotakionter
Boosey Femie 300)
ee)
eer)
Moco Me 2000
Meat Sell M809
ee ee)
jm Hapa Mae 800027. Subquery
-- only need to run this
EXEC Temp_Employee
Subquery in Select
SELECT EmployeeID, Salary, (SELECT AVG(Salary) FROM
EmployeeSalary) AS AllavgSalary
FROM EmployeeSalary
with Partition By
SELECT EmployeeID, Salary, AVG(Salary) OVER () AS
AllavgSalary
FROM EmployeeSalary
Subquery in From
SELECT a.EmployeeID, AllAvgSalary
FROM (SELECT EmployeeID, Salary, AVG(Salary) OVER () AS
AllavgSalary
FROM EmployeeSalary) a
ORDER BY a. EmployeeID
Subquery in Where
SELECT EmployeeID, JobTitle, Salary
FROM EmployeeSalary
WHERE EmployeeID in (SELECT EmployeeID FROM
EmployeeDemographics
WHERE Age > 30
SELECT EmployeeID, JobTitle, Salary
FROM EmployeeSalary
WHERE Salary in (SELECT Max(Salary) FROM EmployeeSalary25. String Functions
== Remove space
Select EmployeeID, TRIM(EmployeeID) AS IDTRIM
FROM EmployeeErrors
Select EmployeeID, RTRIM(EmployeeID) as IORTRIM
FROM EmployeeErrors
Select EmployeeID, LTRIM(EmployeeID) as IDLTRIM
FROM EmployeeErrors
-- Replace
Select LastName, REPLACE(LastName, '- Fired’, ‘') as
LastNameFixed
FROM EmployeeErrors
= Substring
Select Substring(err. FirstName, 1,3),
Substring (dem. FirstName, 1,3), Substring(err.LastName,1,3),
Substring (dem.LastName,1,3)
FROM EmployeeErrors err
JOIN EmployeeDemographics dem
on Substring(err.FirstName,1,3)
Substring (dem. FirstName, 1,3)
and Substring(err.LastName,1,3) =
Substring (dem. LastName ,1,3)
--_UPPER and LOWER CASE
Select firstname, LOWER (firstname)
from EmployeeErrors
Select Firstname, UPPER (FirstName)
from EmployeeErrors”
26. Stored Procedure
CREATE PROCEDURE i=) Employee
AS
DROP TABLE IF EXISTS #temp_employee
Create table #temp_employee (
JobTitle varchar(1@e) ,
EmployeesPerJob int ,
Avgage int,
AvgSalary int
)
Insert into #temp_employee
SELECT JobTitle, Count(JobTitle), Avg(Age), AVG(salary)
FROM EmployeeDemographics emp
JOIN EmployeeSalary sal
‘ON emp.EmployeeID = sal. EmployeeID
where - make sure to change this in
this script from original above
group by JobTitle
Select *
From #temp_employeeStructured Query language (SQL)
‘SQL Commands
De (deal with access — ici
oL eine database | | mu manicustedats| gh and dntaconvat| |, LIGenswithhe | | oa reieve date
Schema in DBMS) Dresentinthe os) "| | onthe data presen in io omg Legere
the db) —
CREATE inser rant commit select
ROP uPoaTe REVOKE rowsacr
aren DELETE
1
2,
3.
Create database
Use the database
[ create database sample2
use sample2
Create table
Insert values into table
Display record from table
create table customer
customerid int identity(1.1) primary key,
customernumber int riot null unique check (customernumber>0)
lastname varchar(30) 0
firstname varchar(30)
areacode int default 71000,
address varchar(50)
country varchar(50) default ‘Malaysia’
insert into customer values
(100,'Fang Ying’ 'Sham’,'418999','sdadasfdfd’ default
(200,'Mei Mei',"Tan' default 'adssdsadsd' ‘Thailand’
300,'Albert’,"John’ default, 'dfdsfsdf', default
display all records
select * from customer
display particular columns
select customerid, customernumber, lastname, firstname
from customer
8
9.
‘Add new column to table
‘Add values to newly added
column/ Update table
Delete a column
Delete record from table
~-if not put ‘where’, will
delete all record
10. Delete table
alter table customer
| add phonenumber_varchar(20
update customer set phonenumber=-"1234545346' where
customerid=1
update customer set phonenumber="45554654" where
| customerid=2
alter table customer
drop column phonenumber
delete
from customer
where country-" Thailand’
[drop table customer
11. Change data type
alter table customer
alter column phonenumber varchar(1@)