SQL Intro: The Beginner's Guide to SQL Basics

Devanshu Agarwal

Written by Devanshu Agarwal /

Introduction

Structured Query Language, commonly known as SQL, is a programming language designed for managing and manipulating data stored in relational databases. It is the backbone of many modern applications that rely on storing, retrieving and managing large amounts of data.

One simple example of how SQL can be used is for retrieving data from a database. Let's say you have a table called "customers" which contains information about your customers, such as their name, email address, and location. You can use SQL to retrieve all the data from this table by running a SELECT statement:

SQL
SELECT * FROM customers;

This statement will return all the data from the "customers" table, including the name, email address, and location of each customer.

SQL is a powerful tool for managing and manipulating data, and its applications are nearly limitless. Whether you're building a simple blog or a complex e-commerce platform, SQL is an essential part of any data-driven application.

SQL Syntax

SQL
SELECT * 
FROM customers 
WHERE country = 'USA' 
ORDER BY last_name ASC;

In this example, we're selecting all columns (*) from the customers table where the country column is equal to 'USA'. We're then ordering the results by last_name in ascending order.

The SELECT statement is used to retrieve data from one or more database tables. The FROM clause specifies the table or tables from which to retrieve the data. The WHERE clause is used to filter the results based on a condition. The ORDER BY clause is used to sort the results in ascending or descending order.

Sure, here's an example table that corresponds to the SQL syntax provided:

idfirst_namelast_nameemailphonecountry
1JohnDoe[email protected]25USA
2JaneSmith[email protected]30USA
3BobJohnson[email protected]40USA
4MaryDavis[email protected]35UK
5TomWilson[email protected]45UK

This table has five columns: id, first_name, last_name, email, and age, and it contains five rows of data. The id column is a unique identifier for each row in the table. The other columns contain information about each person, such as their first name, last name, email address, and age.

Output of the above query

idfirst_namelast_nameemailphonecountry
1JohnDoe[email protected]25USA
3BobJohnson[email protected]40USA
2JaneSmith[email protected]30USA

The query selects all columns (*) from the customers table where the country is equal to 'USA', and then sorts the results by last_name in ascending order (ASC). The resulting table only includes the three rows where the country is 'USA', and those rows are sorted by last_name.