Tag: query

  • 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

  • 12. How to save your query results in a temporary table

    #BigQuery

    Before you run the query, select MORE from the Query Editor and open the Query settings menu.

    This will open:

  • 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.

  • 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: