0% found this document useful (0 votes)
6 views2 pages

"IT" "2Fl. 2B01": Concat Upper

Uploaded by

Syi an
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)
6 views2 pages

"IT" "2Fl. 2B01": Concat Upper

Uploaded by

Syi an
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/ 2

-- Part 0: RECAP of Basic DML Commands

-- INSERT a new record


INSERT INTO department (dnumber, dname, location)
VALUES (7, "IT", "2Fl. 2B01");

-- UPDATE an existing record


UPDATE project
SET dept_no = 2
WHERE pnumber = 2;

-- DELETE an existing record


DELETE FROM department
WHERE location IS NULL;

-- SELECT command: Retrieve all columns from a table


SELECT *
FROM department;

-- Part I: SQL Functions with SELECT

-- Example: Use of CONCAT and UPPER string functions


SELECT CONCAT(fname, " ", lname) AS "Full Name", UPPER(dname) AS "Department"
FROM employee e
INNER JOIN department d ON e.dept_no = d.dnumber;

-- Example: Numeric functions


SELECT salary, ROUND(salary) AS RoundedSalary, FLOOR(salary) AS FloorSalary, CEIL(salary) AS CeilSalary
FROM employee;

-- Example: Date functions


SELECT bdate, (2024 - YEAR(bdate)) AS Age, DATEDIFF(CURDATE(), bdate) AS DaysSinceBirth
FROM employee;

-- Part II: Aggregated Functions with GROUP BY and HAVING

-- Aggregate Functions: COUNT, MAX, MIN


SELECT COUNT(*) AS TotalEmployees FROM employee;
SELECT MAX(salary) AS HighestSalary, MIN(salary) AS LowestSalary FROM employee;

-- GROUP BY with aggregate functions


SELECT dept_no, AVG(salary) AS AvgSalary, SUM(salary) AS TotalSalary
FROM employee
GROUP BY dept_no;

-- GROUP BY with HAVING clause


SELECT proj_no, SUM(hours * hourly_rate) AS TotalPaid
FROM assignment
GROUP BY proj_no
HAVING TotalPaid < 1000;

-- Part III: SELECT with JOINs

-- CROSS JOIN: Cartesian product (for demonstration purposes, rarely practical)


SELECT *
FROM project
CROSS JOIN department;

-- INNER JOIN: Combine rows with matching criteria


SELECT p.pnumber, p.pname, d.dname, d.location
FROM project p
INNER JOIN department d ON p.dept_no = d.dnumber;

-- INNER JOIN with multiple tables


SELECT e.fname, e.lname, d.dname, p.pname
FROM employee e
INNER JOIN department d ON e.dept_no = d.dnumber
INNER JOIN assignment a ON e.ssn = a.essn
INNER JOIN project p ON a.proj_no = p.pnumber;
-- Practical Queries (Based on Assignment)

-- 1. Top 10 oldest professors


SELECT CONCAT(fname, " ", lname) AS Name, (2024 - YEAR(bdate)) AS Age
FROM professor
ORDER BY Age DESC
LIMIT 10;

-- 2. Average GPA of CIS students


SELECT ROUND(AVG(gpa), 2) AS "Average CIS GPA"
FROM student
WHERE major = "CIS";

-- 3. Count of 3-credit courses for selected departments


SELECT COUNT(*) AS "# of Courses"
FROM course
WHERE credits = 3 AND dept_code IN ("CIS", "MATH", "BIOL", "ENG");

-- 4. Total students in selected departments


SELECT dept_code, COUNT(*) AS "Total Students"
FROM student
WHERE dept_code IN ("CIS", "MATH", "BIOL", "ENG")
GROUP BY dept_code;

You might also like