Kaggle: Intro to SQL
Notebook
- SQL, is a programming language used with databases.
- BigQuery: A web service to apply SQL to huge datasets.
- In BigQuery each dataset it contained in a corresponding project.
- To access a dataset:
Construct a reference to the dataset with thedataset()method. Here's how:dataset_ref = client.dataset("hacker_news", project="bigquery-public-data") dataset = client.get_dataset(dataset_ref) - Then we use
get_dataset()method to fetch the dataset. - Every dataset is a collection of tables. Dataset is like spreadsheets.
list_tablesmethod to list the tables in the dataset.tables = list(client.list_tables(dataset))- Print names of all tables in the dataset
for table in tables: print(table.table_id) - Fetch a table from reference:
# Construct a reference to the "full" table table_ref = dataset_ref.table("full") # API request - fetch the table table = client.get_table(table_ref) -
- The structure of a table is called schema.
- Each SchemaField tells us about a specific column(which is also referred to as field).
NULLABLEmeans the a column allows NULL values and is the default.list_rows()can be used to test the first few lines to make sure it's correct, for example:# Preview the first five lines of the "full" table client.list_rows(table, max_results=5).to_dataframe()- Sometimes databases have outdated descriptions, so it's good to check.