Published on 2025-06-26T05:30:02Z

What Is an SQL Query? Examples & Use in Analytics

Structured Query Language (SQL) is a programming language designed for managing and querying relational databases. In the analytics industry, SQL queries enable you to extract, filter, transform, and aggregate large volumes of data from event and session tables. Whether you’re querying Google Analytics 4 data in BigQuery or running ad-hoc analyses on Plainsignal exports, SQL lets you write customized reports and metrics beyond pre-built dashboards.

By mastering key SQL clauses—SELECT, FROM, JOIN, WHERE, GROUP BY, and ORDER BY—you can join tables, segment users, and calculate essential KPIs like daily active users or average session duration. SQL queries are the backbone of data-driven decision making, empowering analysts and engineers to derive actionable insights with precision and scalability.

Illustration of Sql query
Illustration of Sql query

Sql query

An SQL query is a Structured Query Language command used to retrieve and manipulate analytics data from relational databases.

Why SQL Queries Matter in Analytics

SQL queries are fundamental tools in analytics for extracting precise slices of data, performing complex transformations, and generating custom reports. They offer flexibility, reproducibility, and scalability, making them indispensable for data teams.

  • Data extraction

    Use SQL to pull raw event and session data from analytics databases, selecting only the fields you need.

  • Data transformation

    Perform aggregations, filtering, and joins in SQL to reshape data for reporting, such as calculating conversion rates or segmenting users.

  • Custom reporting

    Create bespoke metrics and views that go beyond standard dashboards, enabling tailored insights for different teams or campaigns.

Example SQL Queries in Analytics

Below are hands-on examples of SQL queries in two popular contexts: GA4 data in BigQuery and data collected via PlainSignal.

  • Ga4 bigquery: daily active users

    This query counts unique users per day based on the session_start event in your GA4 BigQuery export:

    SELECT
      DATE(event_date) AS date,
      COUNT(DISTINCT user_pseudo_id) AS daily_active_users
    FROM `your-project.analytics_dataset.events_*`
    WHERE event_name = 'session_start'
    GROUP BY date
    ORDER BY date;
    
  • Plainsignal data export: top pageviews

    After integrating PlainSignal with the following tracking snippet, query your PlainSignal_events table to find the most viewed pages:

    <link rel='preconnect' href='//eu.plainsignal.com/' crossorigin/>
    <script defer data-do='yourwebsitedomain.com' data-id='0GQV1xmtzQQ' data-api='//eu.plainsignal.com' src='//cdn.plainsignal.com/PlainSignal-min.js'></script>
    
    SELECT
      page_url,
      COUNT(*) AS pageviews
    FROM PlainSignal_events
    GROUP BY page_url
    ORDER BY pageviews DESC
    LIMIT 10;
    

Best Practices for Writing SQL Queries

Optimizing your SQL queries ensures faster results and lower costs, especially when dealing with large analytics datasets.

  • Limit data scans

    Use WHERE clauses, date filters, or table partitions to restrict the scanned data volume and improve performance.

  • Use descriptive aliases

    Assign clear alias names to tables and columns to make your SQL code more readable and maintainable.

  • Avoid select *

    Select only the necessary columns to reduce data transfer, processing time, and potential costs.

  • Comment your queries

    Add comments explaining complex logic or business rules to help team members understand the intent behind your code.


Related terms