You are on page 1of 2

MAIN FORMULA

Select * From T1(main table) INNER JOIN T2 ON T1.c = T2.c INNER JOIN T3 ON T1.c = T3.c
To show ALL RECORDS of 3 tables
mysql> select * from tbl_employee INNER JOIN tbl_department ON
tbl_employee.dept_code=tbl_department.dept_code INNER JOIN tbl_status ON
tbl_employee.stat_code=tbl_status.stat_code;

To show LNAME, FNAME, DEPT_CODE, STAT, and SALARY in 3 tables// use specific column in a
table to avoid ambiguous error
mysql> select emp_lname,emp_fname,tbl_department.dept_code, stat_desc, stat_salary from
tbl_employee INNER JOIN tbl_department ON tbl_employee.dept_code=tbl_department.dept_code
INNER JOIN tbl_status ON tbl_employee.stat_code=tbl_status.stat_code;

Order By function// ASC is automatic if not stated what function of order is to be


executed
mysql> select emp_lname,emp_fname,tbl_department.dept_code, stat_desc, stat_salary from
tbl_employee INNER JOIN tbl_department ON tbl_employee.dept_code=tbl_department.dept_code
INNER JOIN tbl_status ON tbl_employee.stat_code=tbl_status.stat_code order by emp_lname
ASC;

Alias function “AS”


// use double quotation “” for not string values
mysql> select emp_lname AS "Last Name", emp_fname AS "First Name", dept_name AS
Department, stat_desc AS Status, stat_salary AS Salary from tbl_employee INNER JOIN
tbl_department ON tbl_employee.dept_code=tbl_department.dept_code INNER JOIN tbl_status
ON tbl_employee.stat_code=tbl_status.stat_code order by emp_lname ASC;

Concat Function (concatenate) and Alias


// use “, “ to add comma between names
mysql> select concat (emp_lname, ", ", emp_fname) AS "Full Name", emp_fname AS "First
Name", dept_name AS Department, stat_desc AS Status, stat_salary as Salary from
tbl_employee INNER JOIN tbl_department ON tbl_employee.dept_code=tbl_department.dept_code
INNER JOIN tbl_status ON tbl_employee.stat_code=tbl_status.stat_code order by emp_lname
ASC;

Annual Salary
mysql> SELECT concat(emp_lname,",", emp_fname) AS "Full Name", tbl_employee.dept_code AS
"Department", stat_desc AS "Status", stat_salary AS "Salary" , stat_salary*12 AS "Annual
Salary" from tbl_employee INNER JOIN tbl_department ON tbl_employee.dept_code =
tbl_department.dept_code INNER JOIN tbl_status ON tbl_employee.stat_code =
tbl_status.stat_code ORDER BY "Salary" ASC;

To show records of WANG and LEE


mysql> SELECT concat(emp_lname,",",emp_fname) AS "Full Name", tbl_employee.dept_code AS
"Department", stat_desc AS "Status", stat_salary AS "Salary" , stat_salary*12 AS "Annual
Salary" from tbl_employee INNER JOIN tbl_department ON tbl_employee.dept_code =
tbl_department.dept_code INNER JOIN tbl_status ON tbl_employee.stat_code =
tbl_status.stat_code where tbl_department.dept_code = "IT" and stat_desc = "Permanent"
ORDER BY "Salary" ASC;

AGGREGATED Function - grouping records together; output is number


 Count()- counts number of
records  Max()- maximum value
 Min()- minimum value  Avg()- average value
Sample Aggregated Function
mysql> SELECT COUNT (emp_id) FROM tbl_employee;
mysql> SELECT AVG(stat_salary) AS “Salary Average”, MIN(stat_salary) AS “Minimum Salary”,
MAX(stat_salary) AS “Maximum Salary” FROM tbl_status;

LIKE RANDOM %= RANDOM


mysql> SELECT emp_lname AS "Last Name", emp_fname AS "First Name",
tbl_department.dept_code AS Department, stat_desc AS Status FROM tbl_employee INNER JOIN
tbl_department ON tbl_employee.dept_code=tbl_department.dept_code INNER JOIN tbl_status
ON tbl_employee.stat_code=tbl_status.stat_code WHERE emp_lname LIKE "%AN%";

SELECT DISTINCT
SELECT DISTINCT tbl_department.dept_code as department FROM tbl_employee.dept_code=
tbl_department.dept_code;

VIEW NUMBER OF IT IN TWO COLUMN


SELECT DISTINCT tbl_department.dept_code as department, COUNT(EMP_LNAME) AS “no of emp”
FROM tbl_employee INNER JOIN tbl_department on tbl_employee.dept_code=
tbl_department.dept_code;

GROUP COUNT PER DEPT_CODE


SELECT DISTINCT tbl_department.dept_code as department, COUNT(EMP_LNAME) AS “no of emp”
FROM tbl_employee INNER JOIN tbl_department on tbl_employee.dept_code=
tbl_department.dept_code GROUP BY tbl_department.dept_code;

You might also like