Basics: Most Commonly used Queries.

Basics: Most Commonly used Queries.

A few basic SQL queries for the record, that are frequently used to retrieve, analyze, and manipulate data stored in databases.

Here are some commonly used queries (in the context of retail for examples, my usual experience and simplicity ) :

1. Basic Data Retrieval:

  • Retrieve basic information from a table.


SELECT * FROM products;
        

2. Filtering Data:

  • Retrieve specific data based on certain conditions.


SELECT * FROM orders WHERE order_status = 'Shipped';
        

3. Sorting Data:

  • Retrieve data sorted in ascending or descending order.


SELECT * FROM customers ORDER BY last_name ASC;
        

4. Aggregate Functions:

  • Perform calculations on data, like calculating total sales.


SELECT SUM(order_total) AS total_sales FROM orders;
        

5. Grouping and Aggregation:

  • Group data based on a column and perform aggregate functions.


SELECT category, COUNT(*) AS product_count FROM products GROUP BY category;
        

6. Joins:

  • Combine data from two or more tables based on related columns.


SELECT orders.order_id, customers.customer_name
FROM orders
INNER JOIN customers ON orders.customer_id = customers.customer_id;
        

7. Subqueries:

  • Use a query result as input for another query.


SELECT product_name
FROM products
WHERE category_id IN (SELECT category_id FROM categories WHERE category_name = 'Electronics');
        

8. Filtering with Date and Time:

  • Retrieve data based on date and time conditions.


SELECT * FROM orders WHERE order_date >= '2022-01-01' AND order_date < '2022-02-01';
        

9. Updating Records:

  • Modify existing data in a table.


UPDATE products SET price = price * 1.1 WHERE category = 'Apparel';
        

10. Inserting Records:

  • Add new data to a table.


INSERT INTO customers (customer_name, email) VALUES ('John Doe', 'john@example.com');
        

11. Deleting Records:

  • Remove data from a table.


DELETE FROM customers WHERE customer_id = 101;
        

12. Combining Multiple Conditions:

  • Use logical operators to combine conditions.


SELECT * FROM products WHERE price > 50 AND stock_quantity > 0;
        

13. Top N Records:

  • Retrieve the top N records based on a specific criterion.


SELECT * FROM products ORDER BY sales DESC LIMIT 10;
        

These SQL queries provide a foundation for managing and extracting valuable insights from retail databases. Depending on specific business requirements, these queries can be customized and extended to suit the analytical needs of a retail environment. What are the top queries that you use daily? feel free to discuss/reach out if you have any questions.

To view or add a comment, sign in

More articles by Aditya Dabrase

  • Ecom x Sentiment Analysis

    Intro: Sentiment analysis in e-commerce is immensely valuable as it allows businesses to gain insights from large…

  • EDA x Retail / E-commerce

    Business Insights Through Exploratory Data Analysis in eCommerce Introduction In today’s competitive retail landscape…

    1 Comment
  • Statistical Distributions: Types and Importance.

    This article is about: Understanding the Normal Distribution What are some other significant distributions? What can we…

  • Sampling & Bias

    The need for sampling: Managing large datasets efficiently. Gaining initial insights into data through exploratory…

  • ANOVA in Experimental Analysis

    Backstory first: ANOVA, or Analysis of Variance, originated from the pioneering work of Sir Ronald Fisher in the early…

  • Hypothesis testing 101

    Hypothesis testing, including significance testing, is performed to make statistically sound conclusions about…

  • Multi-arm bandit Algorithm.

    Rewards-Maximized, Regrets -Minimized! Imagine you're in a casino facing several slot machines (one-armed bandits)…

  • Query Optimization (Joins and Subqueries-Best Practices)

    When working with complex data sets, joins and subqueries are essential tools for retrieving and analyzing data. they…

  • SQL Joins: A Retail Perspective

    Joins are a fundamental concept in SQL, allowing you to combine data from multiple tables to gain valuable insights. In…

  • Subqueries and Derived Tables

    Explaining The concept of subqueries, this article is about how they can be effectively used, using examples from my…

Insights from the community

Others also viewed

Explore topics