0% found this document useful (0 votes)
32 views3 pages

ZakiNaufal 61490 Modul09

The document contains 14 sections of SQL queries that retrieve and manipulate data from database tables like employees, jobs, orders, and products. The queries perform operations like selecting columns, joining tables, grouping, aggregating counts, and updating/rolling back table data within transactions and savepoints.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views3 pages

ZakiNaufal 61490 Modul09

The document contains 14 sections of SQL queries that retrieve and manipulate data from database tables like employees, jobs, orders, and products. The queries perform operations like selecting columns, joining tables, grouping, aggregating counts, and updating/rolling back table data within transactions and savepoints.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

-- 01

SELECT CONCAT(employees.FirstName," ", employees.LastName) 'Name', depts.DeptName


FROM employees
INNER JOIN depts ON employees.DeptID = depts.DeptID
order by DeptName asc;

-- 02
SELECT CONCAT(employees.FirstName," ", employees.LastName) 'Employee Name',
CONCAT(e2.FirstName," ", e2.LastName) 'Manager Name'
FROM employees
Left JOIN employees e2 on employees.ManagerID = e2.EmployeeID
where employees.ManagerID is not null
ORDER BY CONCAT(e2.FirstName," ", e2.LastName) asc,
CONCAT(employees.FirstName," ", employees.LastName) asc;

-- 03
SELECT CONCAT(employees.FirstName," ", employees.LastName) "Name",
GROUP_CONCAT(DISTINCT OrderID
ORDER BY OrderID asc
SEPARATOR ' - ') "Orders"
from orders
LEFT JOIN employees on employees.EmployeeID=orders.EmployeeID
GROUP BY orders.EmployeeID
ORDER BY CONCAT(employees.FirstName," ", employees.LastName) asc;

-- 04
SELECT
products.ProductName "Product Name",
count(products.ProductName) "Total Sold"
FROM orderdetails INNER JOIN
products ON orderdetails.ArticleID = products.ArticleID
GROUP BY products.ProductName
ORDER BY count(products.ProductName) desc;

-- 05
SELECT
products.ProductName AS 'Product Name',
count(orderdetails.OrderID) AS 'Total Sold'
FROM products
LEft JOIN orderdetails
ON products.ArticleID = orderdetails.ArticleID
GROUP BY
products.ProductName
ORDER BY
count(orderdetails.OrderID) DESC;

-- 06
SELECT
DISTINCT(orders.OrderID),
sum(orderdetails.Price),
CONCAT(employees.FirstName," ", employees.LastName) "In Charge"
FROM orders
INNER JOIN orderdetails ON orders.OrderID = orderdetails.OrderID
INNER JOIN employees ON orders.EmployeeID = employees.EmployeeID
GROUP BY OrderID
order by CONCAT(employees.FirstName, employees.LastName) asc,
sum(orderdetails.Price) desc;

-- 07
SELECT
CONCAT(employees.FirstName," ", employees.LastName) AS Employee,
ROUND(sum(orderdetails.Price) * 12/100,0) AS Contribution,
ROUND(jobs.Salary * 5/100,0) AS Bonus
FROM orders
INNER JOIN orderdetails ON orders.OrderID = orderdetails.OrderID
INNER JOIN employees ON orders.EmployeeID = employees.EmployeeID
INNER JOIN jobs ON employees.JobID = jobs.JobID
GROUP BY orders.EmployeeID
HAVING sum(orderdetails.Price)* 12/100 > 500000
ORDER BY sum(orderdetails.Price) DESC;

-- 08
SELECT CONCAT(RPAD(CONCAT(e.FirstName, ' ', e.LastName), 30, " "),
RPAD(CONCAT("works as a ", j.JobDesc), 48, " "),
CONCAT("in ", d.DeptName, " Department")) AS "Employees Information"
FROM employees e
JOIN jobs j USING (JobID)
JOIN depts d USING (DeptID)
ORDER BY j.JobDesc, d.DeptName, CONCAT(e.FirstName, ' ', e.LastName);

-- 09
SET autocommit=0;
SHOW VARIABLES WHERE Variable_name='autocommit';

-- 10
START TRANSACTION;
UPDATE jobs set Salary =0;
SELECT * from jobs;

SAVEPOINT ikea_gaji_nol;
UPDATE employees set FirstName ='Tommy';
SELECT * from employees;

ROLLBACK TO SAVEPOINT ikea_gaji_nol;


SELECT * from employees;

ROLLBACK;
SELECT * from jobs;
SELECT * from employees;

-- Nomor 11
START TRANSACTION;
UPDATE jobs j
SET j.Salary = 0;
SAVEPOINT ikea_gaji_nol;

-- Nomor 12
START TRANSACTION;
UPDATE jobs j
SET j.Salary = 0;
SAVEPOINT ikea_gaji_nol;
UPDATE employees e
SET e.FirstName = "Tommy";
COMMIT;

-- Nomor 13
START TRANSACTION;
UPDATE jobs j
SET j.Salary = 0;
SAVEPOINT ikea_gaji_nol;
UPDATE employees e
SET e.FirstName = "Tommy";
ROLLBACK TO SAVEPOINT ikea_gaji_nol;
COMMIT;

-- Nomor 14
START TRANSACTION;
UPDATE jobs j
SET j.Salary = 0;
SAVEPOINT ikea_gaji_nol;
UPDATE employees e
SET e.FirstName = "Tommy";
ROLLBACK;
COMMIT;

You might also like