
For an instructor lead, in-depth look at learning SQL click below.
SQL or Structured Query Language is the standard language used to interact with a relational database. Databases store information in tables, and SQL allows you to create, manipulate, and analyze these tables. This is a beginner-friendly guide to help you understand and start using SQL.
Understanding Relational Databases and SQL
A relational database is a type of database that stores and provides access to data points that are related to one another. Relational databases are based on the relational model, an intuitive, straightforward way of representing data in tables. In a relational database, each row in the table is a record with a unique ID, called the key. The columns of the table hold attributes of the data, and each record usually has a value for each attribute, making it easy to establish the relationships among data points.
Here’s a simple SQL code that selects all the data from a table:
1 2 3 |
SELECT * FROM Customers; |
Key SQL Commands
CREATE DATABASE
The CREATE DATABASE statement is used to create a new SQL database.
1 2 3 |
CREATE DATABASE DatabaseName; |
CREATE TABLE
The CREATE TABLE statement is used to create a new table in a database.
1 2 3 4 5 6 7 |
CREATE TABLE TableName ( column1 datatype, column2 datatype, ... ); |
INSERT INTO
The INSERT INTO statement is used to insert new records in a table.
1 2 3 4 |
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...); |
SELECT
The SELECT statement is used to select data from a database. The data returned is stored in a result table, called the result-set.
1 2 3 4 |
SELECT column1, column2, ... FROM table_name; |
Joining Tables
When working with relational databases, you will often need to join tables to get the information you need. Joining tables combines them by their common fields. SQL has several types of joins: INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN.
1 2 3 4 5 6 |
SELECT Orders.OrderID, Customers.CustomerName FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID; |
This SQL journey has just begun for you and there is much more to explore. Start practicing SQL with different data and database types and you’ll soon become comfortable with the basics. Happy querying!