Lost your password? Please enter your email address. You will receive a link and will create a new password via email.


You must login to ask a question.

You must login to add post.

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

RTSALL Latest Articles

Most commonly used SQL Queries | Part-2

Most commonly used SQL Queries | Part-2

In our previous guide, we covered the basics of retrieving data, performing joins, and managing records. However, real-world database management often requires more dynamic data manipulation. Mastering intermediate commonly used sql queries will allow you to build complex backend features and write optimized analytics queries.

In this second part, we will explore some of the most powerful commonly used sql queries, covering nested subqueries, pattern matching, conditional outputs, and combining result sets.

1. Nested Queries (Subqueries)

A subquery is a query nested inside another SQL statement. Subqueries are extremely useful when you need to perform calculations or fetch parameters dynamically to filter the main query. For example, if you want to find all employees who earn more than the company’s average salary:

SELECT first_name, last_name, salary
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);

Here, the inner query (SELECT AVG(salary) FROM employees) executes first to calculate the average salary. The outer query then retrieves only the employees earning above that average figure.

2. Combining Result Sets (UNION vs. UNION ALL)

The UNION operator is used to combine the result sets of two or more SELECT statements into a single output. Each query within the union must have the same number of columns with compatible data types.

// Get a list of all client and supplier contact emails
SELECT email FROM clients
UNION
SELECT email FROM suppliers;

UNION vs. UNION ALL Comparison

FeatureUNIONUNION ALL
Duplicate RemovalYes (removes duplicate rows)No (includes all duplicate rows)
PerformanceSlower (requires a distinct sorting operation)Faster (returns results immediately)
Common Use CaseWhen you need a clean, unique listWhen you want to merge logs or audit tracks

3. Pattern Matching (LIKE & Wildcards)

When searching for text records without knowing the exact string, the LIKE operator combined with wildcards is the standard choice:

  • % (Percent sign): Represents zero, one, or multiple characters.
  • _ (Underscore): Represents exactly one single character.
// Find all customers with Gmail addresses
SELECT customer_name, email 
FROM customers 
WHERE email LIKE '%@gmail.com';

// Find employees whose names start with 'J' and are exactly 4 characters long (e.g. John, Jane)
SELECT first_name 
FROM employees 
WHERE first_name LIKE 'J___';

4. Conditional Logic in Queries (CASE WHEN)

The CASE expression is SQL’s version of the if-else statement. It allows you to evaluate conditions and return specific values directly in your query output. You can read more about conditional logic on the official PostgreSQL Documentation.

SELECT product_name, price,
       CASE 
           WHEN price > 100 THEN 'Expensive'
           WHEN price > 50 THEN 'Moderate'
           ELSE 'Inexpensive'
       END AS price_category
FROM products;

This query evaluates the price of each product and outputs a user-friendly text category alongside the raw data.

Summary

Mastering these intermediate patterns will allow you to build sophisticated search systems, handle data merging, and construct cleaner API payloads. To check the basic syntax of JOINs and aggregations, make sure to read Most Commonly Used SQL Queries (Part 1).

Queryiest

Queryiest

Enlightened

Queryiest – Technology Writer | Software Developer | Digital Learning Enthusiast

Queryiest is a technology writer, software developer, and knowledge-sharing enthusiast passionate about simplifying complex technical concepts for students, professionals, and lifelong learners. With expertise in software development, programming, cybersecurity, artificial intelligence, digital tools, and emerging technologies, Queryiest creates practical, research-driven content that helps readers solve real-world problems. As a regular contributor to RTSALL, Queryiest publishes easy-to-understand guides, coding resources, technology news, career advice, and educational tutorials designed for beginners and professionals alike. Every article focuses on accuracy, clarity, and actionable insights to help readers stay informed in the rapidly evolving digital world. Whether it's programming, software engineering, AI, cybersecurity, online platforms, or digital productivity, Queryiest believes that quality knowledge should be accessible to everyone. The goal is to build a trusted learning resource where readers can discover reliable answers, improve their technical skills, and make informed decisions. Areas of Expertise: Software Development, Programming, Cybersecurity, Artificial Intelligence, Technology News, Coding Interview Preparation, Digital Learning, Productivity Tools, and Online Knowledge Sharing.

Related Posts

Leave a comment

You must login to add a new comment.

1 Comment

  1. […] general query practices such as aggregations, subqueries, or basic commands, read our guide on Commonly Used SQL Queries (Part 2). You can also refer to the official Microsoft SQL Server CONCAT Documentation for more technical […]