What is SQL?
SQL is a standard language for storing, manipulating, and retrieving data in databases. There are many different SQL query tools available that allow users to work with data in different ways. Some of these tools are designed for specific tasks, while others are more general purpose.
One of the most popular SQL query tools is Microsoft SQL Server Management Studio (SSMS). This tool provides a graphical interface for working with data in Microsoft SQL Server databases. It includes a query editor, a results pane, and a number of other features.
Another popular tool is Oracle SQL Developer. This tool is designed specifically for working with Oracle databases. It includes a number of features, such as a query builder, a SQL worksheet, and a PL/SQL debugger.
There are also a number of open-source SQL query tools available. These tools include MySQL Workbench, phpMyAdmin, and phpPgAdmin.
Simple SQL Objects and Queries
SELECT and FROM
Data from the database is retrieved using the SELECT keyword. You have two options: either you view all the data in the table or provide the columns’ names (i.e., the kind of data you want to see, such as customer name or age).
The tables from which the data is to be obtained are specified by the FROM keyword. Consider the scenario where you would want to obtain a list of every customer in your database, along with their names and email addresses. This data is in the Customers table, which is displayed below:
Customer ID | Customer Name | City | Country | Age | Sex | |
1211 | Ankit | xxxx@xxxx.com | Paris | Fr | 32 | Male |
1212 | Pallavi | yyyy@yyyy.com | London | UK | 42 | Female |
1213 | Matt | zzzz@zzzz.com | New York | US | 28 | Male |
1214 | Sahil | aaa@bb.com | Sydney | AU | 32 | Male |
1215 | Aarti | bbb@ccc.com | Washington DC | US | 24 | Female |
Here’s the query we’d use to get a list of all the customer names with their email addresses:
Code: SELECT Customer_Name, Email FROM Customers; |
And the output
Customer Name | |
Ankit | xxxx@xxxx.com |
Pallavi | yyyy@yyyy.com |
Matt | zzzz@zzzz.com |
Sahil | aaa@bb.com |
Aarti | bbb@ccc.com |
WHERE
Let’s say you manage an online store with a US-specific promotion. In this instance, you want to send the email exclusively to US clients. Hence the list you choose should only contain US clients. The WHERE phrase can be used in this situation.
Therefore, if you use this query..
Code: SELECT Customer_Name, Email FROM Customers WHERE Country = ‘US’; |
You’ll witness these outcomes:
Matt | zzzz@zzzz.com |
Aarti | bbb@ccc.com |
The Country column, which is not displayed in the results in this case, is filtered using the WHERE clause to identify specific entries.
Let’s imagine you only want American women to be listed. Use the AND keyword in WHERE to filter for various criteria:
Code: SELECT Customer_Name, Email FROM Customers WHERE Country = ‘US’ AND Sex = ‘FEMALE’; |
Result will be:
Aarti | bbb@ccc.com |
The values for Country and Sex for each entry in the aforementioned query are checked against the criteria in the WHERE clause. Only rows that satisfy both requirements will be returned. Use the OR keyword to retrieve the results if any (or both) of the WHERE conditions are true:
Code: SELECT Customer_Name, Email FROM Customers WHERE Country = ‘US’ OR Sex = ‘FEMALE’; |
This chooses all US-based consumers who are female as well as all US-based customers. Take note of the variations in the outcomes:
Order Id | Item | Quantity | Customer Id | Total Value |
24 | A625 | 1 | 1213 | $100 |
23 | Ae35 | 3 | 1214 | $210 |
14 | A212 | 4 | 1214 | $249 |
12 | AXX1 | 4 | 1213 | $212 |
You need to view the most popular orders and their specifics. List orders by value in decreasing order (10-1, Z-A) as one method of doing this. You may use something similar to this:
Code: SELECT * FROM Orders ORDER BY Total_Value DESC; |
Result will be:
Order Id | Item | Quantity | Customer Id | Total Value |
14 | A212 | 4 | 1214 | $249 |
12 | AXX1 | 4 | 1213 | $212 |
23 | Ae35 | 3 | 1214 | $210 |
24 | A625 | 1 | 1213 | $100 |
The SQL engine is instructed to obtain all columns for a table when a ‘*’ is used after SELECT. The ORDER BY clause specifies the order in which the results will be displayed. Since we explicitly use the DESC keyword, the results are delivered in descending order instead of the typical ascending order (1–10, A–Z).
CASE
The Orders table often contains thousands of records for each company. The total value of each order may not necessarily be something you want to see. Instead, you might want to group them according to value or another criterion.
Use the CASE construct to classify orders as “High” or “Low” value depending on whether the order value is $150 or more or less:
Code: SELECT Order_Id, Total_Value CASE WHEN Total_value > 150 THEN ‘HIGH’ WHEN Total_value < 150 THEN ‘LOW’ ELSE ‘MEDIUM’ END as Order_Category FROM Orders; |
You get
Order Id | Total Value | Other Category |
14 | $249 | High |
12 | $212 | High |
23 | $210 | High |
24 | $100 | Low |
The CASE function evaluates each entry in this query. Based on the first condition (i.e. WHEN.. THEN..) that evaluates to true, rows are given a category. The orders with the IDs 14, 12, and 23 are classified as “HIGH” since they cost more than $150. The word “LOW” is given to the order with ID 24.
In the event that none of the requirements are true for any row, the ELSE statement sets a default return value.
GROUP BY
Often, we may want to group related items together in order to draw insights from huge data sets. By combining the data, we can uncover patterns, behaviours, or metrics that we can utilise to guide our judgments. The SQL GROUP BY clause can be used to accomplish this aggregate, which is referred to as grouping.
The group metric is often calculated using a function. The aggregation functions SUM(), COUNT(), AVG(), MAX(), and MIN are frequently used (). With this, you can carry out a variety of tasks, such as adding up all of a department’s expenses or keeping track of how many people work where.
Imagine that your company has a promotion where the customer who spent the most money on your website receives a free gift. Here, you can utilise the SQL GROUP BY clause to get the client with the highest order value. This is the problem:
Code: SELECT Customer_Id, SUM(Total_Value) FROM Orders GROUP BY Customer_Id ORDER BY 2 DESC; |
Order Id | Sum |
1214 | $459 |
1213 | $312 |
Clearly, Customer Id 1214 is your best client.
The query operates by first choosing each distinct Customer Id value, then employing GROUP BY to determine the sum total for each (i.e. the total value of all their orders). The results are shown by SUM(Total Value) in decreasing order using the ORDER BY 2 DESC clause.
JOIN
We discovered the highest-spending customer’s Customer id in the preceding case. However, no additional customer data, such as name or contact information, is kept in the Orders table. To get the necessary data, you’d need to create another query against the Customers table, or you could just use JOIN.
You can combine data from various tables into one query using the SQL JOIN function. Typically, you will use one or more column values that are shared by both tables to join two tables.
Customer Id can be used as the common field in our situation. The tables for customers and orders will be joined.
Code: SELECT b.Customer_Name, b.Email, a.Customer_Id, SUM(Total_Value) FROM Orders a JOIN Customers b ON a.Customer_id = b.Customer_Id GROUP BY b.Customer_Name, b.Email, a.Customer_Id ORDER BY 4 DESC; |
OutPut
Customer_Name | Customer Id | Sum Total | |
Matt | zzzz@zzzz.com | 1214 | $459 |
Pallavi | yyyy@yyyy.com | 1213 | $312 |
The query now additionally returns the necessary client information. It operates by comparing the Customer Id attributes in both tables, then getting just those rows where the values match. The customer IDs 1212, 1215, and 1216 are not included in the results because there are no rows in the Orders table that match these values.
Use a technique known as an OUTER JOIN if you wish to display all values, including those that don’t match anything in the other table. Although they can be a little difficult to comprehend, joins are one of the most crucial concepts in SQL. Check out this training about SQL JOINs at your leisure. It provides 93 exercises that cover all JOIN kinds (there are several).
Adani Institute of Digital Technology (AIDTM) provides online business analytics courses, which is a one-year-long – Executive Program in Business Analytics that is applicable to the industry (EPBA). Reputable professionals from the business and academic world deliver it. The curriculum minimises disturbance to working professionals while offering an unsurpassed and flexible learning experience.
Conclusion:
Overall, there are many common SQL query tools available for data analysis. The most popular ones include MySQL Workbench, PostgreSQL, Microsoft SQL Server, Oracle SQL Developer, and Toad for SQL Server. Each of these tools has its own strengths and weaknesses, so it is important to research and compare the features of each one before selecting the best one for your needs. Additionally, it is important to consider the cost of the SQL query tool when making a decision. By doing so, you can ensure that you get the most out of your data analysis experience.