Pengajar : Mr. M.Arief Hidayat
Fungsi Order by di gunakan untuk mengurutkan baris pada tabel berdasar kepada suatu kolom. Terdiri dari Pengurutan ASC ( ascending ) dan DESC ( descending )
Bagi yang ingin belajar tentang pemrogaman SQL yang order by silahkan langsung coba koding di bawah ini di SQL server anda, dan jangan lupa Database NorthWind-nya ya....
use Northwind
--tampilkan order teurut sesuai tanggal
select *
from Orders
where ShippedDate is not null
order by ShippedDate
--tampilkan Id,nama lengkap,alamat employee
--terurut berdasarkan nama employee
select EmployeeID as ID, FirstName+' '+LastName as 'Nama Lengkap', Address as alamat
from Employees
order by 2
--tampilkan employeId,nama lengkap, alamat
--terurut berdasar usia
select
*from Employees
delete From Employees where EmployeeID = 24
--tampilkan order detail berdasar nilai penjualan
select OrderID ,ProductID,Quantity, UnitPrice*Quantity as 'nilai penjualan'
from [Order Details]
order by UnitPrice * Quantity
--tampilkan 10 % employee paling muda
select top 20 percent
*from Employees
order by BirthDate desc
--tampilkan employee paling muda ( usia dalam tahun ) yang sama tetap di tampilkan
select top 2 with ties FirstName+''+LastName as 'Nama Lengkap' ,DATEDIFF(YY,BirthDate,GETDATE()) as Usia
from Employees
order by DATEDIFF(YY,BirthDate,GETDATE()) asc
--tampilkan producy
--di grup berdasarkan kategorinya
select CategoryID from Products
group by CategoryID
--tampilkan country asal dan nama employee
--di kelompokan berdasar country
select Country from Employees
group by Country
--tampilkan data penjualan barang perbulan pertahun
select DATEPART(YY,OrderDate)[tahun Penjualan],DATEPART(MM,OrderDate)[Bulan Penjualan] from Orders
group by DATEPART(YY,OrderDate),DATEPART(MM,OrderDate)
--tampilkan negara asal employee dan jumlah employeenya
--group by berdasar country
select Country,COUNT(*) [jumlah employee] from Employees
group by Country
--tampilkan harga product termahal untuk tiap kategori barang
select CategoryID,MAX(UnitPrice)[harga termahal] from Products
group by CategoryID
----tampilkan nama kategori termahal untuk tiap kategori barang
select p.CategoryID, c.CategoryName,MAX(UnitPrice)[harga termahal] from Products p, Categories c
where p.CategoryID = c.CategoryID
group by c.CategoryName, p.CategoryID
--tampilkan total banyaknya barang terjual tiap category
select c.CategoryID [Nama], sum(Quantity) [jumlah] from Categories c, Products p, [Order Details] od
where c.CategoryID = p.ProductID
and
p.ProductID = od.ProductID
group by c.CategoryName,c.CategoryID
Fungsi Order by di gunakan untuk mengurutkan baris pada tabel berdasar kepada suatu kolom. Terdiri dari Pengurutan ASC ( ascending ) dan DESC ( descending )
Bagi yang ingin belajar tentang pemrogaman SQL yang order by silahkan langsung coba koding di bawah ini di SQL server anda, dan jangan lupa Database NorthWind-nya ya....
use Northwind
--tampilkan order teurut sesuai tanggal
select *
from Orders
where ShippedDate is not null
order by ShippedDate
--tampilkan Id,nama lengkap,alamat employee
--terurut berdasarkan nama employee
select EmployeeID as ID, FirstName+' '+LastName as 'Nama Lengkap', Address as alamat
from Employees
order by 2
--tampilkan employeId,nama lengkap, alamat
--terurut berdasar usia
select
*from Employees
delete From Employees where EmployeeID = 24
--tampilkan order detail berdasar nilai penjualan
select OrderID ,ProductID,Quantity, UnitPrice*Quantity as 'nilai penjualan'
from [Order Details]
order by UnitPrice * Quantity
--tampilkan 10 % employee paling muda
select top 20 percent
*from Employees
order by BirthDate desc
--tampilkan employee paling muda ( usia dalam tahun ) yang sama tetap di tampilkan
select top 2 with ties FirstName+''+LastName as 'Nama Lengkap' ,DATEDIFF(YY,BirthDate,GETDATE()) as Usia
from Employees
order by DATEDIFF(YY,BirthDate,GETDATE()) asc
--tampilkan producy
--di grup berdasarkan kategorinya
select CategoryID from Products
group by CategoryID
--tampilkan country asal dan nama employee
--di kelompokan berdasar country
select Country from Employees
group by Country
--tampilkan data penjualan barang perbulan pertahun
select DATEPART(YY,OrderDate)[tahun Penjualan],DATEPART(MM,OrderDate)[Bulan Penjualan] from Orders
group by DATEPART(YY,OrderDate),DATEPART(MM,OrderDate)
--tampilkan negara asal employee dan jumlah employeenya
--group by berdasar country
select Country,COUNT(*) [jumlah employee] from Employees
group by Country
--tampilkan harga product termahal untuk tiap kategori barang
select CategoryID,MAX(UnitPrice)[harga termahal] from Products
group by CategoryID
----tampilkan nama kategori termahal untuk tiap kategori barang
select p.CategoryID, c.CategoryName,MAX(UnitPrice)[harga termahal] from Products p, Categories c
where p.CategoryID = c.CategoryID
group by c.CategoryName, p.CategoryID
--tampilkan total banyaknya barang terjual tiap category
select c.CategoryID [Nama], sum(Quantity) [jumlah] from Categories c, Products p, [Order Details] od
where c.CategoryID = p.ProductID
and
p.ProductID = od.ProductID
group by c.CategoryName,c.CategoryID
0 komentar:
Posting Komentar