SQL statement examples

Updated at:

This topic provides example SQL statements for the SQL template feature in Data Q&A.

Important

Starting January 2, 2025, the SQL template feature will no longer be available in Intelligent Conversation Agent (Tongyi Edition).

Where to write SQL statements

In your Intelligent Conversation Agent workspace, navigate to Table Management > SQL Templates and click Create SQL Template.

Note
  • If the agent incorrectly converts a natural language query into an SQL statement, you can create a SQL template to ensure the query is processed correctly.

  • This feature uses SQL to query your database.

image

SQL statement rules and examples

Rules

  1. SQL is not case-sensitive.

  2. The SELECT statement retrieves data from a database.

  3. The SELECT DISTINCT statement returns only unique values.

  4. The WHERE clause filters records.

  5. The AND and OR operators filter records based on multiple conditions.

  6. The ORDER BY clause sorts the result set.

  7. The LIMIT clause specifies the number of records to return.

  8. Use the LIKE operator in a WHERE clause to search for a pattern in a column.

  9. The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates.

Examples

Capability

Description

Example question

Example SQL

Single/multi-column query

Select one or more columns.

List the names and prices of cars with fuel consumption greater than 8 and power less than 200.

SELECT product_name, retail_price FROM cars WHERE (fuel_consumption > 8) AND (power < 200)

Aggregate function query

Use aggregate functions in the SELECT clause, such as COUNT, SUM, AVG, MAX, and MIN.

List the Gold Award records and their latest award times.

SELECT award_record,

MAX(award_time) FROM award_records WHERE award_received = 'Gold Award' GROUP BY award_record

Value comparison

Use operators such as =, >, <, and != in the WHERE clause.

What is the enrollment quota for positions that require a bachelor's degree and have between 20 and 30 approved applicants?

SELECT enrollment_quota FROM

enrollment_info WHERE approved_applicants > 20 AND approved_applicants < 30 AND degree = 'Bachelor'

Multiple conditions

Use AND or OR to combine multiple conditions in the WHERE clause.

What is the total reservoir capacity in the Yangtze River Basin and the Pearl River Basin?

SELECT SUM(capacity) FROM reservoirs WHERE river_basin = 'Yangtze' OR river_basin = 'Pearl'

Abbreviated query

When querying for a specific value, the natural language input may not need to include the column name.

What are the student IDs and degrees of students in Computer Science or with excellent grades?

SELECT student_id, degree FROM student_info WHERE major_name = 'Computer Science' OR grade = 'Excellent'

Generalization

You can use general terms in your natural language query instead of exact column names.

Which SUVs have a fuel consumption below 5?

SELECT product_name FROM cars WHERE (car_type = 'SUV') AND (fuel_consumption < 5)

Top N

Find the top or bottom N records in a ranked list.

What were the top 10 best-selling provinces in 2023?

SELECT province FROM (SELECT province FROM ai_libai_dealer_sales WHERE YEAR(date) = 2023 ORDER BY sales_amount DESC LIMIT 10) AS sales_states ORDER BY sales_amount DESC

Period-over-period growth

Calculate the change in data from one period to the next.

What was the period-over-period growth in total sales for the Sichuan province, comparing Q3 to Q2 of last year?

SELECT (Q3_sales - Q2_sales) / Q2_sales AS pop_growth FROM (SELECT SUM(CASE WHEN QUARTER(date) = 2 THEN sales_amount ELSE 0 END) AS Q2_sales, SUM(CASE WHEN QUARTER(date) = 3 THEN sales_amount ELSE 0 END) AS Q3_sales FROM ai_libai_dealer_sales WHERE YEAR(date) = YEAR(CURRENT_DATE) - 1 AND province = 'Sichuan' AND QUARTER(date) IN (2, 3)) AS subquery

Year-over-year growth

Calculate the change in data for a period compared to the same period in the previous year.

What was the year-over-year growth in total sales for Dealer A in the first quarter of this year?

SELECT (Q1_sales_this_year - Q1_sales_last_year) / Q1_sales_last_year AS yoy_growth FROM (SELECT SUM(CASE WHEN YEAR(date) = YEAR(CURRENT_DATE) THEN sales_amount ELSE 0 END) AS Q1_sales_this_year, SUM(CASE WHEN YEAR(date) = YEAR(CURRENT_DATE) - 1 THEN sales_amount ELSE 0 END) AS Q1_sales_last_year FROM ai_libai_dealer_sales WHERE dealer_name = 'Dealer A' AND QUARTER(date) = 1) AS subquery