Single Table Query
Scenario
This section uses an example of a product inventory table to describe the basic usage of single table queries, including simple queries, conditional filtering, sorting, limiting the number of results, and aggregate queries.
Example Table Description
This section uses the product inventory table newproducts as an example. The table structure is as follows:
| Field | Type | Description |
|---|---|---|
| product_id | INTEGER | Product ID. |
| product_name | VARCHAR2(60) | Product name. |
| category | VARCHAR2(60) | Product category. |
| quantity | INTEGER | Inventory quantity. |
Creating a Sample Table and Inserting Data
Run the following statements to create a sample table and insert data:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | CREATE TABLE newproducts ( product_id INTEGER, product_name VARCHAR2(60), category VARCHAR2(60), quantity INTEGER ) STORE AS orc; INSERT INTO newproducts VALUES (1502, 'earphones', 'electronics',150); INSERT INTO newproducts VALUES (1601, 'telescope', 'toys',80); INSERT INTO newproducts VALUES (1666, 'Frisbee', 'toys',244); INSERT INTO newproducts VALUES (1700, 'interface', 'books',100); INSERT INTO newproducts VALUES (2344, 'milklotion', 'skin care',320); INSERT INTO newproducts VALUES (3577, 'dumbbell', 'sports',550); INSERT INTO newproducts VALUES (1210, 'necklace', 'jewels', 200); |
Simple Query
Run the SELECT... FROM... statement to obtain results from the database.
1 2 3 4 5 6 7 8 9 10 11 | SELECT category FROM newproducts; category ------------ electronics toys sports jewels books skin care toys (7 rows) |
Filtering the Results
Run the WHERE statement to filter the query result and find the queried part.
1 2 3 4 5 6 | SELECT * FROM newproducts WHERE category='toys'; product_id | product_name | category | quantity ------------+--------------+----------+---------- 1601 | telescope | toys | 80 1666 | Frisbee | toys | 244 (2 rows) |
Sorting the Results
Run the ORDER BY statement to sort query results as desired.
1 2 3 4 5 6 7 8 9 10 11 | SELECT product_id,product_name,category,quantity FROM newproducts ORDER BY quantity DESC; product_id | product_name | category | quantity ------------+--------------+-------------+---------- 3577 | dumbbell | sports | 550 2344 | milklotion | skin care | 320 1666 | Frisbee | toys | 244 1210 | necklace | jewels | 200 1502 | earphones | electronics | 150 1700 | interface | books | 100 1601 | telescope | toys | 80 (7 rows) |
Limiting the Number of Query Results
If you want the query to return only part of the result, you can use the LIMIT statement to limit the number of returned records.
1 2 3 4 5 6 7 8 9 | SELECT product_id,product_name,category,quantity FROM newproducts ORDER BY quantity DESC limit 5; product_id | product_name | category | quantity ------------+--------------+-------------+---------- 3577 | dumbbell | sports | 550 2344 | milklotion | skin care | 320 1666 | Frisbee | toys | 244 1210 | necklace | jewels | 200 1502 | earphones | electronics | 150 (5 rows) |
Aggregate Query
If you want to query data comprehensively, you can use the GROUP BY statement and aggregate functions to construct an aggregated query.
1 2 3 4 5 6 7 8 9 10 | SELECT category, string_agg(quantity,',') FROM newproducts GROUP BY category; category | string_agg -------------+------------ books | 100 electronics | 150 skin care | 320 jewels | 200 toys | 80,244 sports | 550 (6 rows) |
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot