
For an instructor lead, in-depth look at learning SQL click below.
Welcome to our beginner-friendly SQL tutorial! SQL (Structured Query Language) is the most commonly used language for managing and manipulating databases. With SQL, you can write queries, insert and modify data, create and modify tables and a whole lot more. This tutorial will cover core concepts and provide code examples to help you get started.
Getting Started
Before we dive in, it’s important to note that all the commands here should be run in your SQL client’s query window. Many SQL development software options offer free versions that can be used for learning SQL, such as Microsoft SQL Server Management Studio, Oracle’s SQL Developer, and many others.
Basic SQL Queries
The most basic command in SQL is the SELECT statement, which allows you to select data from your database and retrieve it. Let’s take a look at it:
|
1 2 3 |
SELECT * FROM Employees |
In the above code, * means that we want to select all fields from the ‘Employees’ table. To run the command, simply copy it into your SQL client’s query window, and hit the ‘Run’ button.
Filtering Results
You may not always want all records but only those that meet a particular criterion. That’s where the WHERE clause comes in. Look at the example below:
|
1 2 3 |
SELECT * FROM Employees WHERE Salary > 5000 |
This command returns only the employees who earn more than 5000.
Sorting Results
SQL also offers a way to sort your results with the ORDER BY command:
|
1 2 3 |
SELECT * FROM Employees ORDER BY Salary DESC |
In this example, the results would be ordered in descending order by Salary.
Inserting Data
Now, let’s cover how to add data to a table:
|
1 2 3 4 |
INSERT INTO Employees (FirstName, LastName, Salary) VALUES ('John', 'Doe', 4500) |
This will add a new record for an employee named John Doe who earns 4500.
Updating Data
You can also update existing data in a table. Here’s how:
|
1 2 3 4 5 |
UPDATE Employees SET Salary = 5000 WHERE FirstName = 'John' AND LastName = 'Doe' |
This command updates John Doe’s salary to 5000.
Deleting Data
Finally, sometimes you might want to delete some data:
|
1 2 3 |
DELETE FROM Employees WHERE FirstName = 'John' AND LastName = 'Doe' |
This will delete John Doe’s record from the database.
Conclusion
With this tutorial, you’ve taken the first steps to understand SQL and become a powerful database user. This is just the tip of the iceberg and there’s a lot more to explore with real case scenarios, business problems and access to real-time databases. We encourage you to keep practicing using these commands until you feel comfortable. Happy coding!
