Table
orders
6 columns · 1 foreign keys · referenced by 1 tables
Columns
| # | Name | Type | Key |
|---|---|---|---|
| 01 | order_id | SERIAL | PK |
| 02 | cust_id | INT | FK |
| 03 | order_date | TIMESTAMP | |
| 04 | status | VARCHAR(30) | |
| 05 | total | NUMERIC | |
| 06 | shipped_at | TIMESTAMP |
Outgoing references
- cust_id — references another table
Referenced by
Example queries against this table
- Select all orders with NULL shipped_at (not shipped)SQL
SELECT order_id, cust_id, order_date, status FROM orders WHERE shipped_at IS NULL; - Show total revenue from all ordersSQL
SELECT SUM(total) AS total_revenue, COUNT(*) AS order_count, ROUND(AVG(total), 2) AS avg_order_value FROM orders WHERE status = 'completed'; - Find all orders NOT in pending or cancelled statusSQL
SELECT order_id, cust_id, total, status FROM orders WHERE status NOT IN ('pending', 'cancelled') ORDER BY order_date DESC; - Find orders placed in the last 30 daysSQL
SELECT order_id, cust_id, order_date, total FROM orders WHERE order_date >= NOW() - INTERVAL '30 days' ORDER BY order_date DESC;