Lab 2 - Basic DDL and DML in MySQL
Lab 2 - Basic DDL and DML in MySQL
Syntax: Example:
Syntax:
Example:
Data (DML -
SELECT
Statement)
• Retrieve all columns:
• SELECT * FROM students;
• Retrieve specific columns:
• SELECT student_name, degree FROM
students;
Examples: • Filter results with conditions:
• SELECT * FROM students WHERE degree =
'Computer Science’;
• Use comparison operators:
• SELECT * FROM students WHERE age > 20;
• Use logical operators:
• SELECT * FROM students WHERE degree =
'Computer Science' AND age > 18;
• SELECT * FROM students WHERE degree =
'Computer Science' OR age > 20;
More • SELECT * FROM students WHERE age
Examples BETWEEN 18 AND 25;
• Use arithmetic operators:
• SELECT Stu_id, Stu_gpa, Stu_gpa + 0.5 AS
'GPA After Bonus' FROM students;
• SELECT Stu_id, YEAR(CURDATE()) -
YEAR(Stu_dob) AS 'Age' FROM students;
• Common Functions:
• COUNT(), SUM(), AVG(), MIN(),
MAX()
Aggregate • Syntax:
• SELECT
Functions function_name(column_name)
FROM table_name;
• Count rows:
• SELECT COUNT(student_id) FROM students;
• Calculate average:
• SELECT AVG(age) FROM students;
• Find minimum and maximum:
Examples: • SELECT MIN(age), MAX(age) FROM students;
• Calculate the total of a column (e.g.,
SUM):
• SELECT SUM(salary) FROM employees;
• Finding distinct values:
• SELECT DISTINCT degree FROM students;
• Syntax:
• SELECT * FROM table_name
ORDER BY column_name
ASC/DESC;