- Relational Databases
- Writing Basic SQL Queries
- Writing Basic Queries using Knex.js
For this lab you will:
- write SQL statements against a pre-populated database using an online tool. Once you have the correct SQL Statement for each query, write it inside the
queries.sql
file under the appropriate heading. - write API endpoints to manage the
accounts
resource, including database logic
Visit SQL Try Editor at W3Schools.com using the Google Chrome (or Chromium if you use Linux) browser and write SQL queries for the following requirements:
- find all customers with postal code 1010. Returns 3 records.
- find the phone number for the supplier with the id 11. Should be (010) 9984510.
- list first 10 orders placed, sorted descending by the order date. The order with date 1997-02-12 should be at the top.
- find all customers that live in London, Madrid, or Brazil. Returns 18 records.
- add a customer record for "The Shire", the contact name is "Bilbo Baggins" the address is "1 Hobbit-Hole" in "Bag End", postal code "111" and the country is "Middle Earth".
- update Bilbo Baggins record so that the postal code changes to "11122".
Clicking the Restore Database
button in the page will repopulate the database with the original data and discard all changes you have made.
- Write CRUD endpoints for the
accounts
resource. Use thedb
object imported fromdata/dbConfig.js
for database access viaknex
. - Manually test your endpoints with a REST client like
Insomnia
orPostman
to check they are working as expected.
field | data type | metadata |
---|---|---|
id | unsigned integer | primary key, auto-increments, generated by database |
name | string | required, unique |
budget | numeric | required |
The following exercises require research, the concepts needed to complete them have not been covered in class yet.
- Find a query to discover how many different cities are stored in the Customers table. Repeats should not be double counted. Should be 69.
- Find all suppliers who have names longer than 20 characters. Returns 11 records.
- Add a
query string
option to yourGET /api/accounts
endpoint. Thequery string
may containlimit
,sortby
andsortdir
keys. If these keys are provided, use these values to limit and sort theaccounts
which are selected from the database. Reference the docs for sorting and limiting inknex
.
// sample req.query object
{
limit: 5,
sortby: 'id',
sortdir: 'desc'
}