SQL Made Easy: Important Operations and Aggregation Basics

Aman SinghAman Singh
6 min read

About Me

Hey folks! I’m Aman, I am currently pursing 2nd year of BCA Information-Technology and am learning frontend Development with the help of Communities.

Am doing LearnInPublic & BuildInPublic so I’ll be sharing my learning in my Socials and I’ll make contents also by documenting my learnings.

What are Operators in SQL?

Operators are essential tools that allow you to perform various operations on thedata stored in your database. They are used to specify conditions in SQL statements, manipulate individual data items, and return results based on those operations.

Operators can be broadly classified into several categories, each serving a different purpose in SQL queries.

  1. Arithmetic Operators: These operators perform basic arithmetic operations such as addition, subtraction, multiplication, and division. For example:

     SELECT 10 % 3 AS ModulusResult;
    
    • + (Addition): Adds two values.

    • - (Subtraction): Subtracts one value from another.

    • * (Multiplication): Multiplies two values.

    • / (Division): Divides one value by another.

  2. Comparison Operators: They are commonly used in the WHERE clause to filter records. Examples include:

    • = (Equal to): Checks if two values are equal.

        SELECT *
        FROM employees
        WHERE department_id = 10;
      
    • != or <> (Not equal to): Checks if two values are not equal.

        SELECT *
        FROM employees
        WHERE department_id <> 10;
      
    • > (Greater than): Checks if one value is greater than another.

        SELECT *
        FROM employees
        WHERE salary > 50000;
      
    • < (Less than): Checks if one value is less than another.

    • >= (Greater than or equal to): Checks if one value is greater than or equal to another.

    • <= (Less than or equal to): Checks if one value is less than or equal to another.

  3. Logical Operators: These operators are used to combine multiple conditions in SQL statements. They help in creating complex queries by combining simple conditions.

    • AND: Returns true if both conditions are true.

        SELECT *
        FROM employees
        WHERE department = 'Sales' AND salary > 50000;
      
    • OR: Returns true if at least one of the conditions is true.

        SELECT * 
        FROM Employees
        WHERE Department = 'Sales' OR Department = 'Marketing';
      
    • NOT: Reverses the result of a condition.

        SELECT *
        FROM Employees
        WHERE NOT (Department = 'Sales');
      

Other Operators: There are several other operators in SQL that serve specific purposes, such as:

  • LIKE: Used for pattern matching in strings.

      SELECT *
      FROM table_name
      WHERE column_name LIKE 'pattern';
    
  • IN: Checks if a value is within a set of values.

      SELECT column_name(s)
      FROM table_name
      WHERE column_name IN (value1, value2, value3);
    
  • BETWEEN: Checks if a value is within a range of values.

      SELECT column_name(s)
      FROM table_name
      WHERE column_name BETWEEN value1 AND value2;
    
  • IS NULL: Checks if a value is NULL.

      SELECT column_name
      FROM table_name
      WHERE column_name IS NULL;
    

Conclusion

Operators form the backbone of SQL's powerful querying capabilities, allowing you to perform a wide range of operations on your data.

What are Aggregations in SQL?

Aggregations are operations that allow you to perform calculations on multiple rows of data to produce a single summary value.

These operations are essential for analyzing and summarizing large datasets, making it easier to derive meaningful insights. it is commonly used in conjunction with the GROUP BY clause to group rows that have the same values in specified columns into summary rows.

Here are some of the most commonly used aggregation functions in SQL:

  1. COUNT(): This function returns the number of rows that match a specified condition. It is useful for counting the number of entries in a table or the number of entries that meet certain criteria.

     SELECT COUNT(*)
     FROM employees
     WHERE department = 'Sales';
    
  2. SUM(): This function calculates the total sum of a numeric column. It is often used to find the total sales, total salary, or any other numeric total.

     SELECT SUM(salary)
     FROM employees
     WHERE department = 'Sales';
    
  3. AVG(): This function computes the average value of a numeric column. It is useful for finding the average salary, average sales, etc.

     SELECT AVG(salary)
     FROM employees
     WHERE department = 'Sales';
    
  4. MIN(): This function returns the smallest value in a specified column. It can be used to find the minimum salary, minimum age, etc.

     SELECT MIN(salary)
     FROM employees
     WHERE department = 'Sales';
    
  5. MAX(): This function returns the largest value in a specified column. It is useful for finding the maximum salary, maximum sales, etc.

     SELECT MAX(salary)
     FROM employees
     WHERE department = 'Sales';
    

Using Aggregation with GROUP BY

The GROUP BY clause is used to arrange identical data into groups.

For example, if you want to find the total salary for each department, you can use the GROUP BYclause with the SUM() function:

SELECT department, SUM(salary) as total_salary
FROM employees
GROUP BY department;

This query will return the total salary for each department, allowing you to see how much each department is spending on salaries.

Combining Aggregations with Other Clauses

Aggregations can also be combined with other SQL clauses like HAVING to filter groups based on certain conditions. For instance, ifyou want to find departments where the total salary exceeds a certain amount, you can use:

SELECT department, SUM(salary) as total_salary
FROM employees
GROUP BY department
HAVING SUM(salary) > 100000;

This query will only return departments where the total salary is greater than 100,000.

Conclusion

Aggregations in SQL enable you to summarize and analyze your data efficiently. By using functions like COUNT(), SUM(), AVG(), MIN(), and MAX(), along with the GROUP BY and HAVING clauses, you can perform complex data analysis and gain valuable insights from your datasets.

ORDER BY Query in SQL

The ORDER BY clause is used to sort the result set of a query by one or more columns. You can sort the data in ascending (default) or descending order.

Here's a simple example:

SELECT * 
FROM employees
ORDER BY last_name;

This query will return all the employees sorted by their last names in ascending order.

If you want to sort the data in descending order, you can use theDESCkeyword:

SELECT * 
FROM employees
ORDER BY last_name DESC;

You can also sort by multiple columns. For example, if you want to sort by department first and then by last namewithin each department:

SELECT * 
FROM employees
ORDER BY department, last_name;

This query will first sort the employees by their department and then by their last name within each department.

That’s a wrap, folks! Thanks for reading the blog. Hope you found it useful for your learning. Please leave a comment, give it a like, and share it with your friends. Happy Learning everyone!

Blog Recap

  • Operators in SQL are tools for performing various operations on database data, including arithmetic, comparison, logical, and other specific functions like LIKE, IN, and BETWEEN.

  • Aggregations in SQL involve calculations on multiple rows to produce summary values using functions like COUNT(), SUM(), AVG(), MIN(), and MAX(), often with the GROUP BY clause.

  • The ORDER BY clause sorts query results by specified columns in ascending or descending order.


Resources

if you prefer Video format to learn these You can checkout this Youtube Video

1
Subscribe to my newsletter

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

Written by

Aman Singh
Aman Singh

Hey folks! I’m Aman. I'm currently in my second year of BCA in Information Technology and learning frontend development with the help of various communities. I'm doing LearnInPublic and BuildInPublic, so I’ll be sharing my learning on my social media. I'll also create content by documenting my journey.