Write a query to display the employee number, last name, first name, and sum of invoice…
- User
Write a query to display the employee number, last name, first name, and sum of invoice totals for all employees who completed an invoice. Sort the output by employee last name and then by first name (Figure P7.61). from lgemployee table and lg invoice table
- Assistant
SELECT emp.employee_number, emp.last_name, emp.first_name, SUM(inv.total) FROM lgemployee emp INNER JOIN lginvoice inv ON emp.employee_number = inv.employee_number GROUP BY emp.employee_number, emp.last_name, emp.first_name ORDER BY emp.last_name, emp.first_name;
- User
Write a query to display the largest average product price of any brand (Figure P7.62).
- Assistant
SELECT brand, AVG(price) AS average_price FROM products GROUP BY brand ORDER BY average_price DESC LIMIT 1;
- User
Write a query to display the manager name, department name, department phone number, employee name, customer name, invoice date, and invoice total for the department manager of the employee who made a sale to a customer whose last name is Hagan on May 18, 2015 (Figure P7.64).
- Assistant
SELECT mg.manager_name, d.department_name, d.department_phone_number, e.employee_name, c.customer_name, i.invoice_date, i.invoice_total FROM employees e INNER JOIN departments d ON e.department_id = d.department_id INNER JOIN invoices i ON e.employee_id = i.employee_id INNER JOIN customers c ON i.customer_id = c.customer_id INNER JOIN employees mg ON d.manager_id = mg.employee_id WHERE c.customer_last_name = 'Hagan' AND i.invoice_date = '2015-05-18';
- User
Write a query to display the brand ID, brand name, brand type, and average price of products for the brand that has the largest average product price (Figure P7.63).
- Assistant
SELECT brand_id, brand_name, brand_type, AVG(price) AS average_price FROM products WHERE brand_type = ( SELECT brand_type FROM products GROUP BY brand_type ORDER BY AVG(price) DESC LIMIT 1 ) GROUP BY brand_id, brand_name, brand_type;
- User
Write a query to display the brand ID, brand name, brand type, and average price of products for the brand that has the largest average product price (Figure P7.63). from lg table
- Assistant
SELECT l.brand_id, l.brand_name, l.brand_type, AVG(p.price) AS average_price FROM lgtable l INNER JOIN lgproduct p ON l.brand_id = p.brand_id WHERE l.brand_type = ( SELECT l2.brand_type FROM lgtable l2 INNER JOIN lgproduct p2 ON l2.brand_id = p2.brand_id GROUP BY l2.brand_type ORDER BY AVG(p2.price) DESC LIMIT 1 ) GROUP BY l.brand_id, l.brand_name, l.brand_type;
- User
Write a query to display the manager name, department name, department phone number, employee name, customer name, invoice date, and invoice total for the department manager of the employee who made a sale to a customer whose last name is Hagan on May 18, 2015 (Figure P7.64). from lgtable
- Assistant
SELECT mg.manager_name, d.department_name, d.department_phone_number, e.employee_name, c.customer_name, i.invoice_date, i.invoice_total FROM lgtable e INNER JOIN lgtable d ON e.department_id = d.department_id INNER JOIN lgtable i ON e.employee_id = i.employee_id INNER JOIN lgtable c ON i.customer_id = c.customer_id INNER JOIN lgtable mg ON d.manager_id = mg.employee_id WHERE c.customer_last_name = 'Hagan' AND i.invoice_date = '2015-05-18';
- User
I have this data but I don't have manager table CREATE TABLE lgbrand ( brand_id numeric(4,0) NULL, brand_name VARCHAR(100) NULL, brand_type VARCHAR(20) NULL );
CREATE TABLE lgproduct ( prod_sku VARCHAR(15) NOT NULL, prod_descript VARCHAR(255) NULL, prod_type VARCHAR(255) NULL, prod_base VARCHAR(255) NULL, prod_category VARCHAR(255) NULL, prod_price NUMERIC(10,2) NULL, prod_qoh NUMERIC(10,0) NULL, prod_min NUMERIC(10,0) NULL, brand_id NUMERIC(4,0) NULL );
- Assistant
Based on the provided table structure, here's the query to display the manager name, department name, department phone number, employee name, customer name, invoice date, and invoice total for the department manager of the employee who made a sale to a customer whose last name is Hagan on May 18, 2015:
SELECT mg.emp_fname || ' ' || mg.emp_lname AS manager_name, d.dept_name, d.dept_phone, e.emp_fname || ' ' || e.emp_lname AS employee_name, c.cust_fname || ' ' || c.cust_lname AS customer_name, i.inv_date, i.inv_total FROM lgemployee e INNER JOIN lginvoice i ON e.emp_num = i.employee_id INNER JOIN lgcustomer c ON i.cust_code = c.cust_code INNER JOIN lgdepartment d ON e.dept_num = d.dept_num INNER JOIN lgemployee mg ON d.emp_num = mg.emp_num WHERE c.cust_lname = 'Hagan' AND i.inv_date = '2015-05-18';Please note that this query assumes that the emp_num column in the lgemployee table represents the employee ID and the dept_num column in the lgdepartment table represents the department ID.