SQL statement examples

Updated at:

This document provides examples of SQL statements for the data Q&A SQL template feature.

Important

Starting January 2, 2025, you will no longer be able to configure SQL templates in Intelligent Chatbot (Tongyi Edition).

Access the SQL statement editor

In the Intelligent Chatbot space, choose table management > SQL template, and then click Create SQL Template.

Note
  • If the chatbot fails to convert a natural language query into the correct SQL statement, you can write the statement manually. This improves the accuracy of the Q&A results.

  • This feature focuses on SQL queries.

image

SQL statement rules and examples

Rules

  1. SQL is case-insensitive.

  2. Use the SELECT statement to select data from a database.

  3. Use the SELECT DISTINCT statement to return only unique values.

  4. Use the WHERE clause to filter records.

  5. Use the AND and OR operators to filter records based on more than one condition.

  6. Use the ORDER BY keyword to sort the result set.

  7. Use the SELECT TOP clause to specify the number of records to return.

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

  9. Use the BETWEEN operator to select values within a given range. The values can be numbers, text, or dates.

Examples

Capability

Description

Sample question

Sample SQL

Single- or multi-column query

Select multiple columns by using SELECT.

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 ( combined_fuel_consumption > "8" ) AND ( power < "200" )

Aggregate function query

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

What are the records for the Gold Award, and what is the latest award date?

SELECT award_record, MAX(award_time) FROM award_records_table WHERE award_won = 'Gold Award' GROUP BY award_record

Value comparison

Use comparison operators such as equal to, greater than, less than, and not equal to in the WHERE clause.

For positions requiring a bachelor's degree, what is the number of planned hires if the count of candidates who passed review is between 20 and 30?

SELECT planned_hires FROM recruitment_info_table WHERE passed_review_count > 20 AND passed_review_count < 30 AND degree = 'Bachelor'

Multiple conditions

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

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

SELECT SUM(capacity) FROM reservoirs WHERE basin = "Yangtze" OR basin="Pearl"

Auto-fill column names

The system can infer the column name from the queried value, so you do not need to explicitly state it in your natural language question.

Which students major in Computer Science or have excellent grades? What are their student IDs?

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

Generalization

Your natural language query does not need to use the exact column name from the table, as the system understands common variations.

Which SUVs have a fuel consumption below 5?

SELECT product_name FROM cars WHERE ( car_type == "suv" ) AND ( combined_fuel_consumption < "5" )

Top N

Finds the top or bottom N items in a result set.

What are the top 10 provinces by sales 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 (PoP) growth

Measures the change in a value between two consecutive periods.

What was the quarter-over-quarter (QoQ) sales growth for Sichuan province in Q3 of last year?

SELECT (Q3_sales - Q2_sales) / Q2_sales AS QoQ_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 (YoY) growth

Measures the change in a value between the current period and the same period in the previous year.

What is the year-over-year (YoY) sales growth for the dealer 'Mao Long' 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 = 'Mao Long' AND QUARTER(date) = 1) AS subquery