Tag: SQL

  • 16. How to write a SQL subquery in BigQuery

    #SQL #subquery # BigQuery

    A subquery is a query that appears inside another query statement.

    Example: Identify which food industries have the highest number of complaints/recalls from the FDA public database and find out which of these complaints led to hospitalizations.

    The first step is to find which food industries have the highest number of complaints.

    Open BigQuery Console and in your project bigquery-public-data, select the fda_food dataset and the food-events table.

    This is a table preview

    Write a query to find which products-industry-name has the highest number of complaints

    Write a query to find out which of these complaints led to hospitalizations:

    The subquery (in yellow) is a query nested in another query to find which products-industry-name has the highest number of complaints. Among these, the main query selects those that led to hospitalizations. The logical AND operator displays a record if all the conditions are TRUE; the LIKE operator searches for a specific pattern in a column: it returns all products_industry_name in the outcomes column that contain the pattern Hospitalization).

    Reference: https://www.w3schools.com/sql/sql_wildcards.asp

  • 15. How to use SQL Join functions in BigQuery

    #SQL #Join #BigQuery

    Example:

    Import the CSV UTF-8 tables, in this example (CUSTOMERS) and (ORDERS), into BigQuery.

    Add the two tables in a dataset (SALES) of the project (myprojectjoin11022026):

    Write a Query with a LEFT JOIN

    o and c are alias for ORDERS and SALES tables.

    Select the desired fields with a LEFT JOIN to obtain

    A LEFT JOIN means:

    Return all rows from the ORDERS table.

    Include data from CUSTOMERS only if a matching record exists.

    If no match exists → the fields from table c are NULL.

    The key is the Customer_ID. This Left join includes all customer_IDs from the Order table (1, 2, 5) and doesn’t include (null) customer_ID #4 from the Customers table as it is not a match.

  • 11. How to Make Sure Your Data Matches Its Description in BigQuery

    #datavalidation #datadescritption #BigQuery

    Based on a hands-on activity of Google Data Analytics Professional issued by Coursera

    File > Download (csv file) the reference table automobile_data

    Create a new data set: open Big Query console, go to the Explorer pane in your workspace to add data

    Create a table (first create a project and a dataset)

    Get an initial understanding of the data table by clicking on the PREVIEW tab

    Enlarged view

    Check the data according to a given data description.

    Requirement 1: the fuel_type column should only have two unique string values: diesel and gas.

    Requirement 2: Length column should range from 141,1 to 208,1.

    Requirement 3: manage null data in the number of doors column.

    Select all lines with a null value in num_of_doors colum with a query

    Update this null value with four (4) with this query:

    Note that Update operation requires a billing account (you can skip). Repeat the same for mazda

    Requirement 4: check potential errors for number of cylinders column

    “tow” in line 7 is a spell-check error. It should be “Two”

    Requirement 5: ensure consistency

    Getting data from a table using SELECT statements.

    De-duplicating data using commands like DISTINCT and COUNT + WHERE.

    Manipulating string data with TRIM to remove extra spaces in a string and SUBSTR to get a portion of a string.

    Creating/dropping tables with CREATE TABLE and DROP TABLE per create or delete tables.

    Changing data types with CAST TO Convert the results of an expression to the given type.

  • 5. How to query public data with SQL

    #BigQuery #SQL #publicdata

    Open Google BigQuery Console and in the Explorer pane, select the Add + button.

    In the pop-up window select Public dataset:

    Search a subject in the Marketplace field, e.g. “job”, to see US job related dataset

    Select a dataset:

    Select the View dataset button, BigQuery opens Public dataset in Explorer pane

    Type your search term in the search text field, e.g. employment, to see the available tables:

    Select the first table to see the employment hours earnings table structure.

    Click on Preview to see the table content:

    Select query in new tab:  

    Write the following query to see the average value for these hour earnings

    SELECT AVG(value) FROM `bigquery-public-data.bls.employment_hours_earnings` LIMIT 1000

    This result can be validated with the related xls file

  • 4. How to write a simple SQL Query in Big Query

    #SQL #queries #BigQuery

    Starting from the custom table I created here, click on Query:

    Start writing your query from this pane

    Question: Which are the Top 5 Most Common male Birth Names in Italy in 1999?

    In SQL pane write:

    SELECT

      nome,

      nati

    FROM `nasciteitalia.datinasciteItalia.diffusionenomi`

    WHERE

      anno=1999

    ORDER BY

      nati DESC

    LIMIT

      5

    This query selects from the data table ‘diffusionenomi’ in database ‘datinasciteItalia’ in project ‘nasciteitalia’ the  two columns (‘nome’ and ‘nati’) for the year ‘1999’ and sorts these two columns in descending order limiting the results to 5 items.

    Click RUN to see the results:

    Check with the reference table from Istat:

  • 2. How to create a free Google BigQuery account for SQL knowledge

    To set up a sandbox account:

    1. Visit the BigQuery sandbox documentation page.
    1. Log in to your preferred Google account by selecting the profile icon in the BigQuery menu bar.
    2. Select the Go to BigQuery button on the documentation page.
    3. You’ll be prompted to select your country and read the terms of service agreement.
    4. This will bring you to the SQL Workspace, where you’ll be conducting upcoming activities. By default, BigQuery creates a project for you.

    After you set up your account, the name of the project will be in the banner in your BigQuery console.