
For an instructor lead, in-depth look at learning SQL click below.
If you’re new to the world of databases and SQL, don’t worry! This guide will walk you through the fundamentals of SQL syntax, using examples, to get you started on your journey to becoming an SQL whiz.
What is SQL?
SQL, or Structured Query Language, is a programming language used to manage and manipulate databases. With SQL, you can create tables, insert data, update data, and retrieve data from a database, among other things.
SQL Statements
The most commonly used SQL commands (also known as statements) are SELECT, INSERT, UPDATE, DELETE, WHERE, and ORDER BY. We will go through each of them.
1. SELECT
The SELECT statement is arguably the most commonly used SQL command. It allows you to select data from a database. The statement is typically used in conjunction with other SQL commands to perform more complex tasks.
1 2 3 4 |
-- This command will select all data from a table named Customers SELECT * FROM Customers |
2. INSERT INTO
The INSERT INTO statement is used to insert new records into a table.
1 2 3 4 5 |
-- This command will insert a new customer into the Customers table INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country) VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway') |
3. UPDATE
The UPDATE statement is used to modify existing records in a table.
1 2 3 4 5 6 |
-- This command will update the contact name of a customer in the Customers table UPDATE Customers SET ContactName = 'Alfred Schmidt' WHERE CustomerName = 'Cardinal' |
4. DELETE
The DELETE statement is used to delete existing records in a table.
1 2 3 4 |
-- This command will delete a customer from the Customers table DELETE FROM Customers WHERE CustomerName = 'Cardinal' |
5. WHERE
The WHERE clause is used to filter records, and is added after a SELECT, UPDATE or DELETE statement to specify which records to select, update or delete.
1 2 3 4 |
-- This command will select all customers from the city Stavanger in the Customers table SELECT * FROM Customers WHERE City = 'Stavanger' |
6. ORDER BY
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
1 2 3 4 |
-- This command will select all customers from the Customers table ordered by Country in ascending order SELECT * FROM Customers ORDER BY Country ASC |
SQL can initially seem daunting, but with practice, you’ll soon be writing queries like a pro. Hopefully, this beginner’s guide gives you a sense of the basics. Happy querying!