Skip to main content

Write SQL database queries - tips

Here are some helpful tips when writing SQL database queries in Productsup.

This document provides some tips on writing Structured Query Language (SQL) queries to databases. SQL queries can help you access product data from databases and your data sources. See Import from a database.

In the following example, two tables contain different data on some products.

Table 1. “product_data”

id

product_id

title

type

1

222

book

direct

2

333

table

dropship

3

444

lamp

dropship



Table 2. “product_pricing”

id

product_id

price

1

222

9.99

2

333

69.99

3

444

49.99



Merge the tables and get product data

You can merge the previous tables and extract Productsup product data by writing a query.

Select product data with the select command

To select your data, use the select command followed by a list of attribute names. This command defines which attributes are part of the result.

It would help if you also defined the table you receive this data from using the from command.

SELECT product_data.product_id,
       product_data.title
FROM product_data

In the previous code snippet, the product_id and title columns come from the product_data table.

Merge two tables with the join command

Use the join command to add or merge additional product information from another table.

The on command specifies what the id column is in each table. The query then looks for matches with the Ids between the tables. The data in the first table merges with the second if there is a match.

JOIN product_pricing ON product_data.product_id = product_pricing.product_id

In the previous code snippet, the product_pricing merges with the product_data table. In this example, the on command specifies that the Id column for both tables is product_id.

Add conditions when you select data with the where command

You can specify conditions to fulfill for exporting data.

WHERE product_data.type = 'dropship'

The previous code snippet shows how you use the where command to define how the class type equals dropship in the product_data column.

Your final query

Each SQL query should at least include a select and from command. Use the join and where commands as necessary.

Here's an example of the entire query when applying these query tips:

SELECT product_data.product_id,
       product_data.title,
       product_pricing.price
FROM product_data
JOIN product_pricing ON product_data.product_id = product_pricing.product_id
WHERE product_data.type = 'dropship'

After performing this query, you would extract the following data:

product_id

title

price

333

table

69.99

444

lamp

49.99

To learn more about SQL, see W3 Schools SQL Tutorial. Contact support@productsup.com if you have any questions.