Geek Culture
Published in

Geek Culture

Learn to write SQL queries in 5 minutes!

Photo by Glenn Carstens-Peters on Unsplash

Why do we need SQL?

Here are some of the uses of SQL which makes working with databases much easier:

  1. Creating a Database: A Database is a collection of organized data that is stored in the form of tables.
  2. 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.
  3. 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.
  4. Inserting and Updating data: SQL can be used to insert and update the data in a database.
  5. Retrieving data: We can retrieve the required data/information from the database with the help of SQL queries.

Basic Definitions:

Table: Student_Details
  1. 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.
  2. 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.
  3. Column: Each column represents a unique field. For example, the above table consists of fields ID, Name and Age.
  4. 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];
  • 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.
SELECT * FROM Student_Details;
Table: Student_Details

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:

  1. 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];
SELECT Name, Age FROM Student_Details
WHERE Age > 18;
Selecting the records where the Age is greater than 18
SELECT * FROM [Table_Name]
ORDER BY [Column_Name] ASC | DESC;
  • Ascending Order:
SELECT * FROM Student_Details
ORDER BY Age ASC;
ORDER BY Age in ascending order
  • Descending Order:
SELECT * FROM Student_Details
ORDER BY Age DESC;
ORDER BY Age in descending order
SELECT [Column_Name] FROM [Table_Name]
GROUP BY [Column_Name];
Table: Student_Data
SELECT COUNT(ID), Age FROM Student_Data
GROUP BY Age;
Number of students of each Age using GROUP BY

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!

Reach me🤙:

--

--

A new tech publication by Start it up (https://medium.com/swlh).

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store