Let's learn SQL together! (II)

Sreya GhoshSreya Ghosh
6 min read

Hi, This is PhiWhyyy and I am happy to write again. How, have you all been? I missed you all as well! I have been so busy with my work and studying parallel that I couldn’t write the next part of this series. But here I am today! And your girlie scored well in The Business Analytics Certification exam and officially knew Text Mining! Yaaaaayy! What have you all been doing (comment below. I’d love to know!) I’ll be starting directly from where I left off in Let’s Learn SQL Together! (I). So without further adieu

In the previous part of this series, we delved into the basics of SQL and explored various functions that can help us manipulate and analyze data. In this second installment, we will focus on the DISTINCT function and how it can be combined with the COUNT function to count the number of unique values in a table effectively.

Want an updated particular name in your table (fig. 2)(Refer to Part I for the table)

UPDATE AUTHOR SET LASTNAME = "KATTA" FIRSTNAME="LAKSHMI"
WHERE AUTHOR_ID="A2"

Advanced SQL Commands

Now let us explore a few more functions In the previous part of this series, we delved into the basics of SQL and explored various functions that can help us manipulate and analyze data. In this second installment, we will focus on the DISTINCT function and how it can be combined with the COUNT function to count the number of unique values in a table effectively.

The DISTINCT function is a powerful tool that allows us to remove duplicate values from a result set, ensuring that only unique records are returned i.e, DISTINCT removes the duplicates to return only unique values. Let's consider a table called "Languages" that contains the following data: Danish, Danish, Danish, Greek, Danish, Greek. When we execute the query `SELECT Language FROM films;`, the result will show us the distinct values present in the table: Danish and Greek.

Let’s consider a table -

Languages:

Language
Danish
Danish
Danish
Greek
Danish
Greek
SELECT Language
FROM films;
Language
Danish
Greek

Now, let's take this a step further and combine the DISTINCT function with the COUNT function. Can you recall the COUNT function? Check the first blog of this series to avoid further confusion. The COUNT function is used to return the number of records in a specified table or view that match the given criteria. By combining these two functions, we can easily calculate the number of unique values in a table. For example, the query `SELECT COUNT AS count_distinct_countries FROM films;` will return the number of unique countries represented in the "films" table, which will be aliased as "count_distinct_countries. This allows you to count the unique values/entities.

Now how will the combination work?

Quite simple actually!

SELECT COUNT(DISTINCT country) AS count_distinct_countries
FROM films

Our query results will return us with the number of unique countries represented in the films table, aliased as count_distinct_countries. (Check my github to see the full code)

As a skilled academic writer, I am pleased to present this comprehensive research paper on the topic of SQL debugging and common errors encountered while working with SQL. SQL, or Structured Query Language, is a programming language used to manage and manipulate relational databases, and as such, it is a crucial tool for data-driven applications and business intelligence.

One of the most common challenges SQL users face is syntax errors, which can arise due to the precise and often complex nature of SQL syntax. (Poulsen et al., 2020) Studies have shown that some students can never resolve these syntax errors to create valid queries and that they struggle the most with SQL concepts related to GROUP BY and correlated subqueries. Understanding the causes behind SQL query formulation errors is crucial for effectively teaching SQL, as committing errors is a crucial part of the learning process. (Taipalus, 2020).

Exploring SQL Formatting Styles

In the realm of data management, the Structured Query Language stands as a cornerstone, enabling users to interact with and manipulate relational databases with efficiency and precision. As the demand for data-driven insights continues to grow, SQL has become an increasingly sought-after skill, transcending the boundaries of software development and permeating various industries. (Poulsen et al., 2020)

Understanding the nuances of SQL formatting is crucial, as it not only enhances code readability but also contributes to the overall maintainability and efficiency of database operations. Researchers have delved into the common mistakes made by students when working with SQL, shedding light on the need for comprehensive educational resources and automated support to ensure that users, both novice and experienced, can navigate the complexities of this powerful language.

One of the most popular formatting styles is the ANSI/ISO standard, which emphasizes using uppercase for SQL keywords, such as `SELECT`, `FROM`, and `WHERE`, while using lowercase for table and column names. (Lans, 1988) This approach enhances the visual distinction between the language constructs and the data entities, making the code more readable and easier to understand.

Another widely adopted style is the "Camel Case" approach, where table and column names are written in a mix of uppercase and lowercase, with the first letter of each word capitalized.

Some More fundamental features of SQL formatting styles include:

  • Indentation: Proper indentation of SQL statements, such as aligning `SELECT`, `FROM`, `WHERE`, and `JOIN` clauses, helps to visually structure the code and improve readability.
  • Spacing: Consistent use of whitespace, such as spaces between keywords and operands, can enhance the overall clarity of SQL statements.

Some other functions comprise ROUND(), SUM(), AVG() , MAX() , MIN(), COUNT(), GROUP BY, ORDER BY, HAVING, LIMIT, OFFSET, LIKE, etc. (Costa et al., 2023)

As the name suggests, `GROUP BY` is used to group rows that have the same values into summary rows, while `ORDER BY` is used to sort the result set in ascending or descending order.

The `HAVING` clause is often used in conjunction with the `GROUP BY` clause to filter groups based on a specified condition. `HAVING` is often used in conjunction with `GROUP BY` to filter groups based on a specified condition.

The `LIMIT` and `OFFSET` clauses are used to control the number of rows returned and the starting point of the result set, respectively.

Regardless of the specific formatting style adopted, the overarching goal should be to create SQL code that is both readable and maintainable, enabling developers, analysts, and end-users alike to work with databases efficiently and effectively.

That’s it for today! That’s more or less all the basic SQL functions. If you need more on understanding how to tackle complicated queries or formatting or filtering numbers or data or basic sorting make sure to comment that and I’ll bring the short guide as earliest possible! Bye!

0
Subscribe to my newsletter

Read articles from Sreya Ghosh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sreya Ghosh
Sreya Ghosh