Pemrograman-Client-Server-Pertemuan-1.docx
-
Upload
denny-sugianto -
Category
Documents
-
view
3 -
download
0
description
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))!