0% found this document useful (0 votes)
9 views5 pages

Lab6 Comp1168-1

The document contains SQL queries for a lab assignment by student Anuj Moond, focusing on various types of joins including INNER JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN, and NATURAL JOIN across different tables such as vendors, invoices, employees, and departments. It includes specific queries to retrieve vendor and customer information, as well as employee details linked to their departments. The document is structured with questions and corresponding SQL code for each task.

Uploaded by

Anuj Choudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Lab6 Comp1168-1

The document contains SQL queries for a lab assignment by student Anuj Moond, focusing on various types of joins including INNER JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN, and NATURAL JOIN across different tables such as vendors, invoices, employees, and departments. It includes specific queries to retrieve vendor and customer information, as well as employee details linked to their departments. The document is structured with questions and corresponding SQL code for each task.

Uploaded by

Anuj Choudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Comp1168 LabNo2

Name: Moond, Anuj


CRN: 50146
Student Id: 101544121
Date: 2025-03-09
-- Q1

(a) join using WHERE:


SELECT v.vendor_id, v.vendor_name, i.invoice_date, i.invoice_total
FROM ap.vendors v, ap.invoices i
WHERE v.vendor_id = i.vendor_id;

(b) INNER JOIN:


SELECT v.vendor_id, v.vendor_name, i.invoice_date, i.invoice_total
FROM ap.vendors v
INNER JOIN ap.invoices i ON v.vendor_id = i.vendor_id;

(c) using KEYWORD:


SELECT v.vendor_id, v.vendor_name, i.invoice_date, i.invoice_total
FROM ap.vendors v
JOIN ap.invoices i USING (vendor_id);
(d) natural JOIN
SELECT v.vendor_id, v.vendor_name, i.invoice_date, i.invoice_total
FROM ap.vendors v
JOIN ap.invoices i USING (vendor_id);

-- Q2
SELECT e.employee_id, CONCAT(e.first_name, ', ', e.last_name) AS 'Employee
Name', d.department_name
FROM ex.employees e
INNER JOIN ex.departments d ON e.department_number =
d.department_number;

-- Q3

(a)
SELECT employee_id, CONCAT(e.first_name, ' ', e.last_name) AS full_name,
d.department_name
FROM ex.employees e
LEFT OUTER JOIN ex.departments d ON e.department_number =
d.department_number;
(b)
SELECT employee_id, concat(e.first_name, ' ', e.last_name) AS full_name,
d.department_name
FROM ex.employees e
RIGHT OUTER JOIN ex.departments d ON e.department_number =
d.department_number
ORDER BY e.employee_id;

(c)
SELECT employee_id, CONCAT(e.first_name, ' ', e.last_name) AS full_name,
department_name
FROM ex.employees e
LEFT OUTER JOIN ex.departments d ON e.department_number =
d.department_number
WHERE d.department_name IS NULL;
(d)
-- Employees with or without departments
SELECT e.employee_id, CONCAT(e.first_name, ' ', e.last_name) AS full_name,
d.department_name
FROM ex.employees e
LEFT OUTER JOIN ex.departments d ON e.department_number =
d.department_number

UNION

-- Departments with or without employees


SELECT e.employee_id, CONCAT(e.first_name, ' ', e.last_name) AS full_name,
d.department_name
FROM ex.employees e
RIGHT OUTER JOIN ex.departments d ON e.department_number =
d.department_number;

-- Q4
SELECT v.vendor_name, v.vendor_zip_code as ZipCode, CONCAT
(c.customer_first_name, ', ', c.customer_last_name) as CustomerName
FROM ap.vendors v
INNER JOIN om.customers c ON v.vendor_zip_code = c.customer_zip;
-- Q5
SELECT concat(c.customer_first_name, ', ', c.customer_last_name) as
CustomerName, o.order_id, i.title, o.order_date, od.order_qty, i.unit_price,
(od.order_qty * i.unit_price) AS total_price
FROM om.customers c
INNER JOIN om.orders o ON c.customer_id = o.customer_id
INNER JOIN om.order_details od ON o.order_id = od.order_id
INNER JOIN om.items i ON od.item_id = i.item_id
ORDER BY CustomerName;

You might also like