Modul Praktikum SQL: Seleksi dan Manipulasi Data Pertemuan 7
Introduction to SQL Selection Queries SQL (Structured Query Language) is used to manage data in relational databases. • SELECT: The basic command to view data from tables. • Syntax: SELECT field1, field2 FROM table_name; • Example: SELECT * FROM employees;
Using the WHERE Clause WHERE is used to filter records based on conditions. • Syntax: SELECT * FROM table_name WHERE condition; • Example: SELECT * FROM employees WHERE city = 'New York';
Using BETWEEN for Range Queries BETWEEN is used to filter records within a range. • Syntax: SELECT * FROM table_name WHERE column BETWEEN value1 AND value2; • Example: SELECT * FROM products WHERE price BETWEEN 100 AND 500;
Pattern Matching with LIKE LIKE is used to search for a specific pattern in a column. • Syntax: SELECT * FROM table_name WHERE column LIKE '%pattern%'; • Example: SELECT * FROM customers WHERE name LIKE '%John%';
Sorting Data with ORDER BY ORDER BY is used to sort the result set in ascending or descending order. • Syntax: SELECT * FROM table_name ORDER BY column ASC/DESC; • Example: SELECT * FROM employees ORDER BY salary DESC;
Grouping Data with GROUP BY GROUP BY is used to group rows with the same values. • Syntax: SELECT column, COUNT(*) FROM table_name GROUP BY column; • Example: SELECT department, COUNT(*) FROM employees GROUP BY department;
SQL Functions: COUNT, SUM, MAX, MIN SQL provides various functions to perform calculations. • COUNT(): Counts the number of rows. • SUM(): Returns the sum of a numeric column. • MAX(): Returns the maximum value. • MIN(): Returns the minimum value.
String Functions: LEFT, RIGHT, MID String functions are used to manipulate text data. • LEFT(x,n): Extracts the leftmost characters. • RIGHT(x,n): Extracts the rightmost characters. • MID(x,n,z): Extracts a substring starting from a specific position.
Practical Exercises 1. Create a 'penjualan' database with 'barang' table. 2. Insert data into 'barang' table with fields: kode, nama_barang, jenis_barang, harga_beli, harga_jual, stok. 3. Run the provided SQL queries and observe the results.