MySQL Basics - A Comprehensive Guide To DDL and DML With Practical Examples
MySQL Basics - A Comprehensive Guide To DDL and DML With Practical Examples
Let's start by creating two tables: Departments and Students in the Universitydb
database. We will also incorporate AUTO_INCREMENT to automatically generate unique values
for department_id and student_id.
-- In sql
-- How to code?
USE Universitydb;
location VARCHAR(100)
);
age INT,
phone_number VARCHAR(15),
);
-- In sql
-- How to code?
-- In sql
-- How to code?
-- In sql
-- How to code?
-- In sql
-- How to code?
Implicit Insert
-- In sql
-- How to code?
Explicit Insert
-- In sql
-- How to code?
-- In sql
-- How to code?
UPDATE Students
SET age = 23
-- How to code?
-- In sql
-- How to code?
-- In sql
-- How to code?
3.2 Problem: List all students who are either older than 20 or belong to
department 1.
-- In sql
-- How to code?
SELECT * FROM Students WHERE age > 20 OR department_id = 1;
-- In sql
-- How to code?
-- In sql
-- How to code?
3.5 Problem: List all students along with their department names using
INNER JOIN.
-- In sql
-- How to code?
FROM Students s
3.6 Problem: Retrieve all students and their department names, including
students who may not have a department (LEFT JOIN).
-- In sql
-- How to code?
SELECT s.name AS student_name, d.department_name AS department
FROM Students s
3.7 Problem: List all students and their department names, including
departments even if no students are assigned (RIGHT JOIN).
-- In sql
-- How to code?
FROM Students s
-- In sql
-- How to code?
FROM Students s
GROUP BY d.department_name;
3.9 Problem: Count the number of students per department but only for
departments with more than 1 student.
-- In sql
-- How to code?
SELECT d.department_name AS department, COUNT(*) AS student_count
FROM Students s
GROUP BY d.department_name
3.10 Problem: Find all students in the 'Computer Science' department using
a subquery.
-- In sql
-- How to code?
-- In sql
-- How to code?
-- In sql
-- How to code?
FROM Students s
INNER JOIN Departments d ON s.department_id = d.department_id
GROUP BY d.department_name;
-- In sql
-- How to code?
FROM Students s
GROUP BY d.department_name;
-- In sql
-- How to code?
-- In sql
-- How to code?
3.16 Problem: List all student names from department 1 and department 2
using UNION.
-- In sql
-- How to code?
UNION
3.17 Problem: List all student names from department 1 and department 2,
including duplicates, using UNION ALL.
-- In sql
-- How to code?
UNION ALL
3.18 Problem: Find students who are in both department 1 and have ages
greater than 20 using INTERSECT.
-- In sql
-- How to code?
INTERSECT
3.19 Problem: Retrieve the names of students who are in the same
department as student Bob.
-- In sql
-- How to code?
3.20 Problem: Use an alias to retrieve the student name and their
department location in a single query.
-- In sql
-- How to code?
FROM Students s
Single-Row Subqueries
Single-row subqueries return a single row and single column of results. These are typically used
with operators like =, <, >, <=, >=, or <>.
4.1 Problem: Find the student who has the highest age.
-- In sql
-- How to code?
4.2 Problem: Retrieve the student who is younger than the oldest student in the 'Physics'
department.
-- In sql
-- How to code?
4.3 Problem: Find students who are older than the average age of students in the
'Mathematics' department.
-- In sql
-- How to code?
4.4 Problem: Retrieve the student who has an email address with the same domain as
'[email protected]'.
-- In sql
-- How to code?
4.5 Problem: Find students who have a phone number that is not in the format of
'1234567890' (using not equal operator).
-- In sql
-- How to code?
Multiple-Row Subqueries
Multiple-row subqueries return more than one row and are often used with operators like IN,
ANY, ALL, or EXISTS.
4.6 Problem: Find students who are in any of the departments with IDs 1, 2, or 3.
-- In sql
-- How to code?
4.7 Problem: Retrieve students who are in departments with more than 2 students.
-- In sql
-- How to code?
GROUP BY department_id
4.8 Problem: Find students who are not in the same departments as those with age over
23.
-- In sql
-- How to code?
4.9 Problem: Retrieve students who are in departments where the department name
contains 'Science'.
-- In sql
-- How to code?
-- In sql
-- How to code?
Conclusion
This guide provided a comprehensive overview of MySQL basics, including DDL and DML
commands, advanced concepts such as aliases and auto-increment fields, practical examples
of SQL queries for various scenarios, and detailed subquery examples. By understanding these
concepts and practicing the provided examples, you can effectively manage and manipulate
your data in MySQL.