Pemrograman-Client-Server-Pertemuan-1.docx

2
 OVERVIEW OF SQL Relational databases are organized into tables Use a primary key to uniquely identify rows in a table Use a foreign key to link to a dierent table PRODUCTS ProductId ProductName UnitPrice SupplierI d SUPPLIERS Suppli er Id CompanyNa me ddress City !eber apa cara menggabungkan dua tabel pr oducts dan suppli ers" dengan foreign key supplierId #enggunakan $%&R& o S&'&C( ProductName" CompanyName )R*# Products P" Suppliers S $%&R& P+SupplierId , S+SupplierId #enggunakan -*IN o S&'&C( ProductName" CompanyName )R*# Products P -*IN Suppliers S *N .P +SupplierId , S+SupplierId/ #enggunakan Subquery o S&' &C( Pr oductName" CompanyName )R*# Pr odu cts P $%&R& P +SupplierId , .S&'&C( SupplierId" SupplierName )R*# Supplier S/ #enggunakan '&)( -*IN .0ika ada data yang tidak berkaitan foreign key nya/ o S&'& C( Pro ductName" Compa nyName )R*# Suppl ier S '&)( -*IN Products P *N .S+SupplierId , P+SupplierId/ SOAL 1+ !uat quer y yang menampilkan P roduc tId" Pr oduct Name" Uni tPric e dimana 12 3 unitPrice 3 122 dan diurutkan berdasarkan unit Pr ice secara descending4 S&'& C( Pr oductI d" Pr oductName" UnitPr ice )R*# Pr oducts $%&R& UnitPrice3122 N5 UnitPrice612 *R5&R !7 UnitPrice 5&SC 8+ Cari Pr oduct d imana P rod uct bera sal dar i Supplier 'o ndon4 S&'&C( ProductName" Ci ty )R*# Pr oducts P" Suppliers S $%&R& P+SupplierId , S+SupplierId N5 S+City , 9'ondon:

description

Pemrograman-Client-Server-Pertemuan-1.docx

Transcript of Pemrograman-Client-Server-Pertemuan-1.docx

OVERVIEW OF SQL

Relational databases are organized into tables Use a primary key to uniquely identify rows in a table Use a foreign key to link to a different table

PRODUCTS

ProductIdProductNameUnitPriceSupplierId

SUPPLIERS

SupplierIdCompanyNameAddressCity

Beberapa cara menggabungkan dua tabel products dan suppliers, dengan foreign key supplierId Menggunakan WHERE SELECT ProductName, CompanyName FROM Products P, Suppliers S WHERE P.SupplierId = S.SupplierId Menggunakan JOIN SELECT ProductName, CompanyName FROM Products P JOIN Suppliers S ON (P.SupplierId = S.SupplierId) Menggunakan Subquery SELECT ProductName, CompanyName FROM Products P WHERE P.SupplierId = (SELECT SupplierId, SupplierName FROM Supplier S) Menggunakan LEFT JOIN (jika ada data yang tidak berkaitan foreign key nya) SELECT ProductName, CompanyName FROM Supplier S LEFTJOIN Products P ON (S.SupplierId = P.SupplierId)

SOAL1. Buat query yang menampilkan ProductId, ProductName, UnitPrice dimana 10 < unitPrice < 100 dan diurutkan berdasarkan unitPrice secara descending!SELECT ProductId, ProductName, UnitPrice FROM Products WHERE UnitPrice10 ORDER BY UnitPrice DESC

2. Cari Product dimana Product berasal dari Supplier London!SELECT ProductName, City FROM Products P, Suppliers S WHERE P.SupplierId = S.SupplierId AND S.City = London

3. Carilah company yang tidak memiliki product!SELECT SupplierId, CompanyName FROM Suppliers WHERE Products.SupplierId != Suppliers.SupplierId

Use a WHERE clause to filter rows retrieved in a query Filter rows by using search conditions Comparison operators Likeness comparisons Ex : SELECT CompanyName FROM Customers WHERE CompanyName LIKE B%;//diawali huruf B Ex : SELECT CompanyName FROM Customers WHERE CompanyName LIKE %B;//diakhiri huruf B

Logical operators Range of values List of values Unknown values

KUIS KECIL Tampilkan nama panjang (gabungan firstname dan lastname) dan umur dari employee! SELECT CONCAT(Firstname, , LastName) Nama, ROUND(DATEDIFF(NOW(), BirthDate)/365, 0) Usia FROM employees; Tampilkan CategoryID, CategoryName, dan jumlah produk per kategori! SELECT C.CategoryID, CategoryName, COUNT(p.productid) Jumlah Product FROM categories c JOIN Products p ON (c.CateogryID=p.CategoryID) GROUP BY c.CategoryID; Tampilkan data employee dan atasannya! SELECT e1.employeeId, CONCAT(e1.FirstName, , e1.LastName) Nama,e1.ReportsTo, CONCAT(e2.FirstName, , e2.LastName) AtasanFROM employees e1 LEFT JOIN employees e2 ON (e1.ReportsTo = e2.employeeId)

TUGAS Tampilkan penjualan product per bulan (bulan, ProductID, ProductName, Jumlah / SUM(Quantity), Total = SUM(Price * Quantity))! Tampilkan asal negara dari product (ProductID, ProductName, Country)! Tampilkan jumlah pembelian dari customer (CustomerID< CustomerName, JumlahOrder, TotalOrder = SUM(Price * Unit))!