Welcome to the SQL Practice Problems lesson! In this tutorial, we'll work together to enhance your SQL skills with practical examples and exercises. By the end of this lesson, you'll be able to query, manipulate, and analyze data like a pro! 💡
SQL (Structured Query Language) is a standard language used to communicate with and manipulate databases. It allows us to create, modify, and retrieve data stored in a database, as well as perform various operations on it.
Before we dive into SQL queries, let's talk about tables and schemas. A table is similar to a spreadsheet, where data is organized in rows and columns. A schema is a collection of tables that together make up a database.
To create a table, use the CREATE TABLE statement followed by the table name and its columns.
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(100),
age INT,
gender VARCHAR(10)
);To insert data into a table, use the INSERT INTO statement.
INSERT INTO students (id, name, age, gender)
VALUES (1, 'Alice', 25, 'Female');To retrieve data from a table, use the SELECT statement.
SELECT * FROM students;To update data in a table, use the UPDATE statement.
UPDATE students SET age = 26 WHERE id = 1;To delete data from a table, use the DELETE FROM statement.
DELETE FROM students WHERE id = 1;Joining tables allows us to combine data from multiple tables based on a common column. There are four types of JOINs: INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN.
Aggregate functions, like COUNT, SUM, MIN, MAX, and AVG, allow us to perform calculations on groups of data.
Subqueries are nested queries that return a result set, which can then be used in another query.
Now, it's time to test your skills! Try solving these practice problems on your own.
Create a table named courses with columns id, name, description, and price. Insert some sample data.
CREATE TABLE courses (
id INT PRIMARY KEY,
name VARCHAR(100),
description TEXT,
price DECIMAL(10, 2)
);
INSERT INTO courses (id, name, description, price)
VALUES (1, 'Web Development', 'Learn to code and build websites', 999.99);
-- Add more rows here...Retrieve all courses and their descriptions.
SELECT name, description FROM courses;Update the description of the course with ID 1.
UPDATE courses SET description = 'An in-depth look at web development, including front-end and back-end technologies' WHERE id = 1;Find the total number of courses in the database.
SELECT COUNT(*) FROM courses;Find the average price of all courses.
SELECT AVG(price) FROM courses;Which SQL statement is used to insert data into a table?
Keep practicing, and soon you'll be an SQL master! 💡
Happy coding! ✅