MODULO SQL & Database Design Master SeriesLAB

Unlocking the Power of SQL: A Comprehensive Introduction

What is SQL?

SQL (Structured Query Language) is a programming language designed for managing and manipulating data stored in relational database management systems (RDBMS). It is a standard language for accessing, managing, and modifying data in relational databases. SQL is used to perform various operations, such as creating and modifying database structures, inserting, updating, and deleting data, and querying data.

History of SQL

SQL was first developed in the 1970s by a team at IBM's San Jose Research Laboratory. The first publicly available version of SQL was released in 1979. Since then, SQL has undergone several revisions, with the most recent being SQL:2016.

SQL vs NoSQL

SQL and NoSQL are two different approaches to managing data. The main difference between the two is the way they store and manage data.

SQL Databases

SQL databases use a fixed schema to store data. The schema defines the structure of the data, including the relationships between different tables. SQL databases are ideal for applications that require:

  • Complex transactions
  • Strong data consistency
  • Support for complex queries

Examples of SQL databases include MySQL, PostgreSQL, and Microsoft SQL Server.

NoSQL Databases

NoSQL databases, on the other hand, use a variety of data models, such as key-value, document, graph, and column-family stores. NoSQL databases are ideal for applications that require:

  • High scalability
  • High performance
  • Flexible schema

Examples of NoSQL databases include MongoDB, Cassandra, and Redis.

SQL Dialects

SQL dialects are variations of the SQL language that are specific to a particular database management system. While SQL is a standard language, different databases may implement SQL differently. Some common SQL dialects include:

  • T-SQL (Transact-SQL): Used by Microsoft SQL Server
  • PL/SQL (Procedural Language/SQL): Used by Oracle
  • MySQL SQL: Used by MySQL
  • PostgreSQL SQL: Used by PostgreSQL

SQL Syntax

SQL syntax is composed of several elements, including:

  • Commands: SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, etc.
  • Clauses: WHERE, FROM, GROUP BY, HAVING, etc.
  • Functions: SUM, COUNT, AVG, MAX, MIN, etc.
  • Operators: =, <, >, <=, >=, LIKE, IN, etc.

SQL Data Types

SQL supports various data types, including:

  • Integer: whole numbers, e.g., 1, 2, 3, etc.
  • String: character strings, e.g., 'hello', 'world', etc.
  • Date: dates, e.g., '2022-01-01', '2022-12-31', etc.
  • Time: times, e.g., '12:00:00', '23:59:59', etc.
  • Boolean: true or false values

SQL Queries

SQL queries are used to retrieve data from a database. There are several types of SQL queries, including:

  • SELECT: retrieves data from a database table
  • INSERT: inserts new data into a database table
  • UPDATE: updates existing data in a database table
  • DELETE: deletes data from a database table

Example SQL Queries

SELECT Query

SELECT *
FROM customers
WHERE country='USA';

This query retrieves all columns (*) from the customers table where the country column is 'USA'.

INSERT Query

INSERT INTO customers (name, email, country)
VALUES ('John Doe', 'john.doe@example.com', 'USA');

This query inserts a new row into the customers table with the specified values.

UPDATE Query

UPDATE customers
SET name='Jane Doe'
WHERE email='john.doe@example.com';

This query updates the name column of the row in the customers table where the email column is 'john.doe@example.com'.

DELETE Query

DELETE FROM customers
WHERE email='john.doe@example.com';

This query deletes the row from the customers table where the email column is 'john.doe@example.com'.

⚔️ YOUR MISSION

Mission: Create a new database called "library" and create two tables: "books" and "authors". The "books" table should have columns for "id", "title", "author_id", and "published_date". The "authors" table should have columns for "id", "name", and "biography". Insert at least 5 books and 3 authors into the tables. Write a SQL query to retrieve all books written by a specific author.

SQL practice editor
Read the exercise prompt
-- Create the database
CREATE DATABASE library;

-- Use the database
USE library;

-- Create the authors table
CREATE TABLE authors (
  id INT PRIMARY KEY,
  name VARCHAR(255),
  biography TEXT
);

-- Create the books table
CREATE TABLE books (
  id INT PRIMARY KEY,
  title VARCHAR(255),
  author_id INT,
  published_date DATE,
  FOREIGN KEY (author_id) REFERENCES authors(id)
);

-- Insert authors
INSERT INTO authors (id, name, biography)
VALUES
  (1, 'John Doe', 'John Doe is a famous author.'),
  (2, 'Jane Doe', 'Jane Doe is a well-known author.'),
  (3, 'Bob Smith', 'Bob Smith is a popular author.');

-- Insert books
INSERT INTO books (id, title, author_id, published_date)
VALUES
  (1, 'Book 1', 1, '2020-01-01'),
  (2, 'Book 2', 1, '2020-02-01'),
  (3, 'Book 3', 2, '2020-03-01'),
  (4, 'Book 4', 3, '2020-04-01'),
  (5, 'Book 5', 1, '2020-05-01');

-- Retrieve all books written by a specific author
SELECT b.*
FROM books b
JOIN authors a ON b.author_id = a.id
WHERE a.name = 'John Doe';
WAITING FOR SIGNAL...
BACKNEXT STAGE