|
| 1 | +(nlsql-example-product)= |
| 2 | + |
| 3 | +# NLSQL with product orders |
| 4 | + |
| 5 | +Let's use a basic products / orders / customers database. |
| 6 | + |
| 7 | +## Basic JOINs and filtering |
| 8 | + |
| 9 | +:::{rubric} Provision |
| 10 | +::: |
| 11 | + |
| 12 | +Create table and insert data. |
| 13 | +Populate the table using a few records worth of example data. |
| 14 | + |
| 15 | +```sql |
| 16 | +CREATE TABLE customers (customer_id INTEGER, name VARCHAR, city VARCHAR); |
| 17 | +CREATE TABLE orders (order_id INTEGER, customer_id INTEGER, amount INTEGER); |
| 18 | +CREATE TABLE products (product_id INTEGER, name VARCHAR); |
| 19 | +CREATE TABLE order_items (order_id INTEGER, product_id INTEGER); |
| 20 | + |
| 21 | +-- customers |
| 22 | +INSERT INTO customers (customer_id, name, city) VALUES |
| 23 | +(1, 'Alice', 'Berlin'), |
| 24 | +(2, 'Bob', 'Munich'), |
| 25 | +(3, 'Charlie', 'Hamburg'); |
| 26 | + |
| 27 | +-- products |
| 28 | +INSERT INTO products (product_id, name) VALUES |
| 29 | +(1, 'Laptop'), |
| 30 | +(2, 'Phone'), |
| 31 | +(3, 'Headphones'); |
| 32 | + |
| 33 | +-- orders |
| 34 | +INSERT INTO orders (order_id, customer_id, amount) VALUES |
| 35 | +(101, 1, 1200), |
| 36 | +(102, 2, 800), |
| 37 | +(103, 1, 200), |
| 38 | +(104, 3, 150); |
| 39 | + |
| 40 | +-- order_items |
| 41 | +-- Alice bought Laptop, Bob bought Phone, Alice bought Headphones, |
| 42 | +-- Charlie bought Headphones, Charlie also bought Phone. |
| 43 | +INSERT INTO order_items (order_id, product_id) VALUES |
| 44 | +(101, 1), |
| 45 | +(102, 2), |
| 46 | +(103, 3), |
| 47 | +(104, 3), |
| 48 | +(104, 2); |
| 49 | +``` |
| 50 | + |
| 51 | +:::{rubric} Query |
| 52 | +::: |
| 53 | + |
| 54 | +Submit a typical query in human language. |
| 55 | + |
| 56 | +```shell |
| 57 | +ctk query nlsql "List all customers with orders over €500." |
| 58 | +``` |
| 59 | + |
| 60 | +:::{rubric} Response |
| 61 | +::: |
| 62 | + |
| 63 | +The model figures out the SQL statement, the engine runs it, and |
| 64 | +uses the model again to come back with an answer in human language: |
| 65 | + |
| 66 | +> The query results show that the customers 'Alice' from Berlin |
| 67 | +> and 'Bob' from Munich have placed orders over €500. |
| 68 | +
|
| 69 | +The SQL statement was: |
| 70 | +```sql |
| 71 | +SELECT customers.name, customers.city |
| 72 | +FROM customers JOIN orders ON customers.customer_id = orders.customer_id |
| 73 | +WHERE orders.amount > 500; |
| 74 | +``` |
| 75 | + |
| 76 | +## Advanced JOINs and filtering |
| 77 | + |
| 78 | +:::{rubric} Provision |
| 79 | +::: |
| 80 | + |
| 81 | +Create table and insert data. |
| 82 | +Add a few customers in New York and others elsewhere. |
| 83 | +Synthesize orders with amounts both above and below the average. |
| 84 | + |
| 85 | +```sql |
| 86 | +CREATE TABLE customers (customer_id INTEGER, name VARCHAR, city VARCHAR); |
| 87 | +CREATE TABLE orders (order_id INTEGER, customer_id INTEGER, amount INTEGER); |
| 88 | +CREATE TABLE products (product_id INTEGER, name VARCHAR); |
| 89 | +CREATE TABLE order_items (order_id INTEGER, product_id INTEGER); |
| 90 | + |
| 91 | +INSERT INTO customers (customer_id, name, city) VALUES |
| 92 | +(1, 'Alice Johnson', 'New York'), |
| 93 | +(2, 'Bob Smith', 'Los Angeles'), |
| 94 | +(3, 'Carol Lee', 'New York'), |
| 95 | +(4, 'David Brown', 'Chicago'); |
| 96 | + |
| 97 | +INSERT INTO orders (order_id, customer_id, amount) VALUES |
| 98 | +(101, 1, 500), -- NY, high |
| 99 | +(102, 1, 150), -- NY, low |
| 100 | +(103, 2, 300), -- non-NY |
| 101 | +(104, 3, 700), -- NY, high |
| 102 | +(105, 4, 200); -- non-NY |
| 103 | + |
| 104 | +INSERT INTO products (product_id, name) VALUES |
| 105 | +(1001, 'Laptop'), |
| 106 | +(1002, 'Phone'), |
| 107 | +(1003, 'Tablet'), |
| 108 | +(1004, 'Headphones'); |
| 109 | + |
| 110 | +INSERT INTO order_items (order_id, product_id) VALUES |
| 111 | +(101, 1001), |
| 112 | +(101, 1004), |
| 113 | +(102, 1002), |
| 114 | +(103, 1003), |
| 115 | +(104, 1001), |
| 116 | +(104, 1002), |
| 117 | +(105, 1004); |
| 118 | +``` |
| 119 | + |
| 120 | +:::{rubric} Query |
| 121 | +::: |
| 122 | + |
| 123 | +Submit a typical query in human language. |
| 124 | + |
| 125 | +```shell |
| 126 | +ctk query nlsql "Get the names of products that were ordered by customers in New York who spent more than the average amount." |
| 127 | +``` |
| 128 | + |
| 129 | +:::{rubric} Response |
| 130 | +::: |
| 131 | + |
| 132 | +The model figures out the SQL statement, the engine runs it, and |
| 133 | +uses the model again to come back with a synthesized response |
| 134 | +based on the provided SQL query and its result: |
| 135 | + |
| 136 | +> The query identifies the top 10 product names ordered by customers in New York |
| 137 | +> who spent more than the average order amount. |
| 138 | +> The results show that "Laptop", "Phone", and "Headphones" were among the most |
| 139 | +> popular products purchased by New York customers with high spending. |
| 140 | +
|
| 141 | +The SQL statement was: |
| 142 | +```sql |
| 143 | +SELECT |
| 144 | + p.name FROM products AS p |
| 145 | + JOIN order_items AS oi ON p.product_id = oi.product_id |
| 146 | + JOIN orders AS o ON oi.order_id = o.order_id |
| 147 | + JOIN customers AS c ON o.customer_id = c.customer_id |
| 148 | +WHERE |
| 149 | + c.city = 'New York' |
| 150 | +ORDER BY |
| 151 | + o.amount DESC LIMIT 10; |
| 152 | +``` |
| 153 | + |
| 154 | +## JOINs and grouping |
| 155 | + |
| 156 | +:::{rubric} Provision |
| 157 | +::: |
| 158 | + |
| 159 | +```sql |
| 160 | +CREATE TABLE customers (customer_id INTEGER, name VARCHAR, city VARCHAR, email_address VARCHAR, gender_code VARCHAR); |
| 161 | +CREATE TABLE orders (order_id INTEGER, customer_id INTEGER, amount INTEGER); |
| 162 | +CREATE TABLE products (product_id INTEGER, name VARCHAR, price NUMERIC(2), size VARCHAR); |
| 163 | +CREATE TABLE order_items (order_id INTEGER, product_id INTEGER); |
| 164 | + |
| 165 | +INSERT INTO customers (customer_id, name, city, email_address, gender_code) VALUES |
| 166 | +(1, 'Alice Johnson', 'New York', 'alice@example.com', 'F'), |
| 167 | +(2, 'Bob Smith', 'Los Angeles', 'bob@example.com', 'M'), |
| 168 | +(3, 'Carol Lee', 'Chicago', 'carol@example.com', 'F'), |
| 169 | +(4, 'David Brown', 'Houston', 'david@example.com', 'M'), |
| 170 | +(5, 'Eva Green', 'Phoenix', 'eva@example.com', 'F'), |
| 171 | +(6, 'Frank Miller', 'Miami', 'frank@example.com', 'M'), |
| 172 | +(7, 'Grace Kim', 'Seattle', 'grace@example.com', 'F'), |
| 173 | +(8, 'Henry Davis', 'Boston', 'henry@example.com', 'O'); -- least common gender |
| 174 | + |
| 175 | +INSERT INTO orders (order_id, customer_id, amount) VALUES |
| 176 | +(101, 1, 120), |
| 177 | +(102, 2, 200), |
| 178 | +(103, 3, 150), |
| 179 | +(104, 4, 300), |
| 180 | +(105, 6, 80); |
| 181 | + |
| 182 | +INSERT INTO products (product_id, name, price, size) VALUES |
| 183 | +(1001, 'T-Shirt', 20, 'M'), |
| 184 | +(1002, 'Jeans', 50, 'L'), |
| 185 | +(1003, 'Jacket', 80, 'XL'), |
| 186 | +(1004, 'Sneakers', 60, '42'), |
| 187 | +(1005, 'Hat', 15, 'S'); |
| 188 | + |
| 189 | +INSERT INTO order_items (order_id, product_id) VALUES |
| 190 | +(101, 1001), |
| 191 | +(101, 1005), |
| 192 | +(102, 1002), |
| 193 | +(103, 1003), |
| 194 | +(104, 1004), |
| 195 | +(105, 1001); |
| 196 | +``` |
| 197 | + |
| 198 | +:::{rubric} Q & A |
| 199 | +::: |
| 200 | + |
| 201 | +- Q: What are the email address and town of the customers who are of the least common gender? |
| 202 | + SQL: `SELECT email_address, city FROM customers GROUP BY gender_code ORDER BY count(*) ASC LIMIT 1` |
| 203 | +- Q: What are the product price and the product size of the products whose price is above average? |
| 204 | + SQL: `SELECT products.price, products.size FROM products WHERE products.price > (SELECT AVG(price) FROM products)` |
| 205 | +- Q: Which customers did not make any orders? |
| 206 | + SQL: `SELECT c.name FROM customers AS c LEFT JOIN orders AS o ON c.customer_id = o.customer_id WHERE o.order_id IS NULL;` |
0 commit comments