BASICS OF SQL
Structured Query Language (SQL) is a powerful programming language that is used to manage and manipulate data stored in relational databases. Whether you’re a software developer, data analyst, or just interested in learning more about how data is stored and retrieved, SQL is a fundamental skill that you need to know.
In this article, we’ll cover the basics of SQL and teach you how to query databases. We’ll start from the very beginning, assuming no prior knowledge of SQL, and gradually build up your skills until you’re able to write complex queries with confidence.
Understanding Relational Databases
Before we dive into SQL, it’s important to understand what a relational database is and how it’s structured. A relational database is a collection of tables that are related to each other in some way. Each table consists of rows and columns, and each row represents a record in the table, while each column represents a specific piece of information about that record.
For example, let’s say you have a table called “Customers” that contains information about all of the customers who have ever bought products from your company. The table might have columns for the customer’s name, email address, phone number, and shipping address, among other things. Each row in the table represents a different customer, with each column containing specific information about that customer.
Querying Databases with SQL
Now that you understand the basics of relational databases, let’s dive into SQL. SQL is used to query databases by selecting specific data from one or more tables and filtering that data based on certain criteria.
The most basic SQL statement is the SELECT statement, which is used to retrieve data from one or more tables. Here’s an example:
SELECT * FROM Customers;
This statement retrieves all of the data from the Customers table. The asterisk (*) is a wildcard character that represents all columns in the table.
You can also retrieve specific columns by specifying their names in the SELECT statement, like this:
Get Fariz khan’s stories in your inbox
Join Medium for free to get updates from this writer.
SELECT Name, Email FROM Customers;
This statement retrieves only the Name and Email columns from the Customers table.
To filter the data in a table based on certain criteria, you can use the WHERE clause, like this:
SELECT * FROM Customers WHERE Country = ‘USA’;
This statement retrieves all of the data from the Customers table where the Country column is equal to ‘USA’. You can also use comparison operators like <, >, <=, >=, and != to filter data based on numeric or date values.
Sorting Data with SQL
You can sort the data in a table using the ORDER BY clause. Here’s an example:
SELECT * FROM Customers ORDER BY Name;
This statement retrieves all of the data from the Customers table and sorts it in ascending order based on the Name column. You can also sort the data in descending order by adding the DESC keyword, like this:
SELECT * FROM Customers ORDER BY Name DESC;
This statement retrieves all of the data from the Customers table and sorts it in descending order based on the Name column.
Conclusion
SQL is a powerful and versatile programming language that is used to manage and manipulate data stored in relational databases. By understanding the basics of SQL, you can query databases to retrieve specific data, filter that data based on certain criteria, and sort the data to make it easier to analyze. With practice and experience, you can become a SQL hero and unlock the full potential of relational databases.




