Learn to write SQL queries in 5 minutes!
SQL stands for Structured Query Language which is one of the most in-demand skills for Data Science. It is widely used for storing, manipulating and retrieving data in the RDBMS (Relational Database Management System). The SQL commands which are used to retrieve the required data or information from the database are known as SQL queries.
In this article, I will be explaining the uses of SQL, some important definitions and steps to create a query with examples.
Why do we need SQL?
Here are some of the uses of SQL which makes working with databases much easier:
- Creating a Database: A Database is a collection of organized data that is stored in the form of tables.
- Creating Tables: Tables in SQL are the database objects that are used to store the data of a database. In tables, the data is organized in the form of rows and columns where each column represents a unique field and each row represents a unique record.
- Creating Views: The views are also known as virtual tables. They are similar to the real tables of a database. The views can be created by selecting all or the required columns/fields from the various tables in a database.
- Inserting and Updating data: SQL can be used to insert and update the data in a database.
- Retrieving data: We can retrieve the required data/information from the database with the help of SQL queries.
Basic Definitions:
- Table: A table is a collection of data in which the data is organized in the form of rows and columns. For example, the above table that is named as Student_Details having fields ID, Name and Age.
- Row: It is also known as a tuple/record. A single row contains all the attributes that make a single entity. For example, 1, Ram, 18 together represents the details of a single student also known as a single record.
- Column: Each column represents a unique field. For example, the above table consists of fields ID, Name and Age.
- Query: A query is basically a command which is used to retrieve the desired information from one or more tables in a database.
Writing the first SQL query:
The following query can be used to extract all the information from a table:
SELECT * FROM [TABLE_NAME];
Here are the components of a SQL query:
- The SELECT statement is used to select the information from the database which is to be displayed and it is necessary for retrieving the data.
- ( * ) asterisk is used to select all the columns of a table.
- FROM is used to denote the name of the table from which the information is to be retrieved.
We can use this above query to display the entire information from our Student_Details table as follows:
SELECT * FROM Student_Details;
Note: The Table_Name and Column_Name are CASE-SENSITIVE.
Filtering information with some additional conditions:
We can use additional conditions to the above query to filter out the information according to our needs. These are the optional conditions that may or may not be present in the query. They are as follows:
- WHERE: The WHERE clause is used to display only the records which fulfill the provided conditions. We can use various operators like >, < and =, etc. in the WHERE clause.
SELECT [Column_Name] FROM [Table_Name]
WHERE [Condition];
For example, if we want the names of the students whose age is greater than 18 then we can write the query as follows:
SELECT Name, Age FROM Student_Details
WHERE Age > 18;
We can use AND and OR to filter the records based on more than one condition in the WHERE clause and NOT can be used to filter and display the results where the conditions are not satisfied.
2. ORDER BY: The ORDER BY is a keyword that is used to sort the records in ascending or descending order. We can use ASC for sorting the records in ascending order and DESC for sorting the records in descending order.
Note: By default, the ORDER BY keyword sorts the result in the ascending order so it is not necessary to mention ASC if we want the result in the ascending order.
We can use the ORDER BY clause as follows:
SELECT * FROM [Table_Name]
ORDER BY [Column_Name] ASC | DESC;
For example, if we want to select all the records from the Student_Details table and sort them in ascending and descending order according to age then we can write the query as follows:
- Ascending Order:
SELECT * FROM Student_Details
ORDER BY Age ASC;
- Descending Order:
SELECT * FROM Student_Details
ORDER BY Age DESC;
3. GROUP BY: The GROUP BY statement is used to group the rows with the same values into the summary rows. Generally, it is used with the aggregate functions like MIN(), MAX(), COUNT(), SUM() and AVG(). We can use the GROUP BY statement as follows:
SELECT [Column_Name] FROM [Table_Name]
GROUP BY [Column_Name];
For example, let us consider the table:
Here, in the above table, there are two records having the Age of 17 and two records having the Age of 18. So we can use the GROUP BY statement to list the number of students of each age as follows:
SELECT COUNT(ID), Age FROM Student_Data
GROUP BY Age;
Conclusion:
These were the basic steps to create a SQL query. Now you are ready to go and extract information from the database using SQL. The link to some of the best online resources for SQL is provided at the end. For more such upcoming content related to Python, Machine Learning, Data Science and Front-end development follow Chirag Rathi!
Happy Learning!😊
Reach me🤙:
- Visit my portfolio website at chiragrathi.me