SQL Intro: The Beginner's Guide to SQL Basics
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:
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
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:
id | first_name | last_name | phone | country | |
---|---|---|---|---|---|
1 | John | Doe | [email protected] | 25 | USA |
2 | Jane | Smith | [email protected] | 30 | USA |
3 | Bob | Johnson | [email protected] | 40 | USA |
4 | Mary | Davis | [email protected] | 35 | UK |
5 | Tom | Wilson | [email protected] | 45 | UK |
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
id | first_name | last_name | phone | country | |
---|---|---|---|---|---|
1 | John | Doe | [email protected] | 25 | USA |
3 | Bob | Johnson | [email protected] | 40 | USA |
2 | Jane | Smith | [email protected] | 30 | USA |
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
.
Read More
Sorting SQL data in ascending and descending order
Learn how to sort your SQL data in ascending order with the ORDER BY ASC clause and improve your query results.
SQL Practice Queries - 1
Looking to enhance ur DB skills? With our advanced sql queries, you'll learn techniques to improve your database skills.
SQL Practice Queries - 2
Looking to enhance your database management skills and take your SQL knowledge to the next level? With our advanced SQL functions and queries training, you'll learn the syntax and techniques to improv ...
Practical Examples of Using SQL Distinct, WHERE Clause and AND, OR, and NOT operators
See practical examples of how to use the SQL Distinct, WHERE Clause and AND, OR, and NOT operators