
For an instructor lead, in-depth look at learning SQL click below.
Welcome to SQL 101, the series dedicated to guiding you through the basics of SQL. Firstly, what is SQL? SQL stands for Structured Query Language. This is a programming language used for storing, manipulating, and retrieving data stored in relational databases.
Select Statement
The most basic statement in SQL is probably the SELECT statement. It’s used to query the data from a database and retrieve data that matches the criteria that you specify. Here’s the basic syntax:
|
1 2 3 4 |
SELECT column1, column2,... FROM table_name; |
For example, if we have a Customers table, and we want to retrieve all the customer names and their cities, we would write:
|
1 2 3 |
SELECT CustomerName, City FROM Customers; |
Where Clause
How about getting specific records that meet certain criteria? This is where the WHERE clause comes in handy. The WHERE clause is used to filter records and its syntax is:
|
1 2 3 4 5 |
SELECT column1, column2,... FROM table_name WHERE condition; |
For instance, if we want to select only those customers from the city “London”, our query would look like:
|
1 2 3 4 5 |
SELECT CustomerName, City FROM Customers WHERE City='London'; |
Insert Statement
To add new data into a database, we use the INSERT INTO statement. Here’s the basic syntax:
|
1 2 3 4 |
INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...); |
For example, if we want to insert a new customer into our Customers table:
|
1 2 3 4 |
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country) VALUES ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger','4006','Norway'); |
Delete Statement
And of course, if you want to delete any records that match a certain condition, you would use the DELETE statement. Basic syntax:
|
1 2 3 |
DELETE FROM table_name WHERE condition; |
For example, if we want to delete the customer ‘Cardinal’ from our Customers table:
|
1 2 3 |
DELETE FROM Customers WHERE CustomerName='Cardinal'; |
In conclusion, using SQL can be simple or complex, depending on the task at hand. Whether you’re a beginner or a pro, there’s always more to learn. Today, you’ve learned about SELECT, WHERE, INSERT, AND DELETE statements. But, remember, practice makes perfect. Keep learning and keep coding!
