
For an instructor lead, in-depth look at learning SQL click below.
Welcome to the SQL Crash Course where we will dive into the world of relational databases. If you’re a beginner with little to no experience with SQL or databases, don’t worry, by the end of this guide, you’ll have a solid foundation of SQL basics and you’ll be able to create, query, and manipulate data stored in a relational database.
What is SQL?
SQL, or Structured Query Language, is a language designed to manage data held in a relational database management system (RDBMS). It has a variety of functions including querying data, updating data, inserting data, creating and modifying databases and tables, and setting permissions on tables, procedures, and views.
Setting up a database
Let’s start by setting up a sample database which we will use throughout the course. Below is a query to create a database named ‘student_db’.
1 2 3 |
CREATE DATABASE student_db; |
Creating Tables
Once our database is set up, we can create some tables. Here is an example of creating a ‘students’ table with four columns – ‘id’, ‘first_name’, ‘last_name’, and ’email’.
1 2 3 4 5 6 7 8 |
CREATE TABLE students ( id INT PRIMARY KEY, first_name VARCHAR(40), last_name VARCHAR(40), email VARCHAR(50) ); |
Inserting data
With a table in place, we can start inserting data into it. Let’s add three different students to our ‘students’ table.
1 2 3 4 5 6 7 |
INSERT INTO students (id, first_name, last_name, email) VALUES (1, 'John', 'Doe', <a href="mailto:'john.doe@example.com'" >'john.doe@example.com'</a>), (2, 'Jane', 'Doe', <a href="mailto:'jane.doe@example.com'" >'jane.doe@example.com'</a>), (3, 'Jim', 'Brown', <a href="mailto:'jim.brown@example.com'" >'jim.brown@example.com'</a>); |
Querying data
What good is a database if we can’t retrieve the data, right? We use SELECT query for this purpose. For example, to get all the data from the ‘students’ table, we would use the following query:
1 2 3 |
SELECT * FROM students; |
In SQL, * is a wildcard character that represents ‘all’. So, SELECT * means select all columns. If we wanted to select only certain columns, we could replace * with the column names, separated by commas. For example, if we wanted to select only first name and email, our query would look like this:
1 2 3 |
SELECT first_name, email FROM students; |
Wrapping Up
This is the tip of the iceberg when it comes to SQL, but with these basics, you can already do a lot. Practice creating your own databases and tables, inserting data, and writing queries, and you’ll be well on your way to becoming proficient in SQL! In the next section, we will cover more advanced topics like JOINs, aggregate functions, subqueries, and more. So, stay tuned!