Skip to content

Instantly share code, notes, and snippets.

@mroswell
Last active January 19, 2024 04:54
Show Gist options
  • Save mroswell/0668fc4c6e51158ddccf4ef496fb3251 to your computer and use it in GitHub Desktop.
Save mroswell/0668fc4c6e51158ddccf4ef496fb3251 to your computer and use it in GitHub Desktop.

Based on the transcript you provided, it seems like the speaker mentioned several SQL queries during the webinar. I'll extract these and format them as SQL queries for you. Here are the possible SQL queries from the transcript:

  1. Basic Query to Show All Rows in a Table:

    SELECT * FROM demo_table;
  2. Query to Show Individuals Over 40 Years of Age:

    SELECT * FROM spatial_sql_webinar.demo_table WHERE age > 40;
  3. Update Query to Set Age for a Specific ID:

    UPDATE spatial_sql_webinar.demo_table SET age = 50 WHERE id = 1;
  4. Insert Query to Add a New Record:

    INSERT INTO spatial_sql_webinar.demo_table (name, age) VALUES ('Peter', 47);
  5. Delete Query for a Specific ID:

    DELETE FROM spatial_sql_webinar.demo_table WHERE id = 6;
  6. Create a New Table:

    CREATE TABLE spatial_sql_webinar.demo_table2 (id INT, name VARCHAR, age INT);
  7. Buffer Function Query:

    SELECT id, ST_Buffer(geom, 500) AS geom FROM spatial_sql_webinar.windsor_community_centers;
  8. Intersect Analysis Query:

    SELECT a.* FROM spatial_sql_webinar.windsor_parcels a 
    JOIN spatial_sql_webinar.windsor_community_centers b 
    ON ST_Intersects(a.geom, ST_Buffer(b.geom, 500));
  9. Area Calculation for Specific Zone:

    SELECT *, ST_Area(geom) AS area_sqm FROM spatial_sql_webinar.windsor_parcels 
    WHERE zoning LIKE 'A%';
  10. Total Area Calculation with Aggregate Function:

    SELECT ROUND(SUM(ST_Area(geom)), 2) AS sum_area_sqm FROM spatial_sql_webinar.windsor_parcels 
    WHERE zoning LIKE 'A%';
  11. Length Calculation for a Specific Street:

    SELECT SUM(ST_Length(geom)), COUNT(*), ROUND(MAX(ST_Length(geom)), 2), ROUND(MIN(ST_Length(geom)), 2)
    FROM spatial_sql_webinar.windsor_streets 
    WHERE st_name LIKE 'Richmond%';
  12. Perimeter Calculation for Specific Zone:

    SELECT *, ROUND(ST_Perimeter(geom), 2) AS perimeter_meters FROM spatial_sql_webinar.windsor_parcels 
    WHERE zoning LIKE 'A%' ORDER BY perimeter_meters;

These queries represent a range of basic and spatial SQL operations that were likely discussed in the webinar. Please note that actual field names and table structures may vary.

https://www.youtube.com/watch?v=V9rwY86rMKQ&t=928s
```
SELECT *
From table_name;
SELECT *
From table_name
WHERE age > 40;
UPDATE spatial_sql_webinar.demo_table
SET age = 50
WHERE id = 1;
INSERT INTO spatial_sql_webinar.demo_table
(name, age)
VALUES ('Peter', 47);
DELETE FROM spatial_sql_webinar.demo_table
WHERE id = 6;
# slightly different
CREATE TABLE spatial_sql_webinar.demo_table2 (
id serial primary key,
name VARCHAR,
age INT
);
# spelling slightly different
SELECT
id,
ST_Buffer(geom, 500) AS geom
FROM spatial_sql_webinar.windsor_community_centers;
# Customized to SpatiaLite
SELECT
id,
Buffer(MakePoint(longitude, latitude, 4326), 500) AS geom
FROM
spatial_sql_webinar.windsor_community_centers;

It seems that the sqlparse module, which is typically used for formatting SQL statements, is not available in this environment. However, I can manually format the SQL statements to make them more readable. Let's proceed with that approach.

I have manually formatted the SQL statements for better readability. Here's the updated table with the formatted SQL:

Slide Title Description Formatted SQL
Advanced SQL Date handling, subqueries and more None
Link to these slides http://bit.ly/nicarsql4 None
Getting started Open DB Browser for SQLite, if it’s not open already, and open the osha.sqlite file None
Dates in SQLite SQLite stores dates as text, unlike some other relational databases. To treat them as dates, use the date() function. SELECT MIN(DATE(open_date)), MAX(DATE(open_date))
FROM inspections
Extracting date parts To extract parts of the date -- year, month, day, etc. -- use the strftime() function. To select the year ('%Y'): SELECT strftime('%Y', open_date)
FROM inspections
Extracting date parts To select the month ('%m'): SELECT strftime('%m', open_date)
FROM inspections
To get the day, you’d specify '%d' -- see the full list of directives here.
Quick date format check The strftime function will return NULL if you attempt to parse an invalid date. You can use it to run some limited integrity checks on columns with date values. SELECT *, strftime('%Y', open_date) AS date_test
FROM inspections
WHERE date_test IS NULL
Using date parts in a query Another trick with strftime: Extracting part of a date on the fly and using the resulting value elsewhere in your query. For instance, if you wanted to group inspections by year: SELECT strftime('%Y', open_date) AS 'Year', COUNT(*)
FROM inspections
GROUP BY 1
ORDER BY 2 DESC
Exercise in small groups Practice grabbing pieces of the date from the close_date column and grouping by year. None
Finding the difference between dates Use the function Julianday() to convert the date string into the number of the day according to the Julian calendar (the number of days since noon in Greenwich on November 24, 4714 B.C.). Once the dates are converted to numbers, you can simply subtract them: SELECT julianday(close_case_date) - julianday(open_date) AS days_open
FROM inspections
ORDER BY 1 desc
Converting Julian date values to years To convert the Julian date difference to years (roughly), simply divide the number by 365.25: SELECT (julianday(close_case_date) - julianday(open_date)) / 365.25 AS years_open
FROM inspections
ORDER BY 1 desc
Subqueries A subquery is simply a full SQL query that is nested inside another query. We’ll go through a few examples of when nested queries can come in handy. First, a few general tips: When writing nested queries, it’s helpful to write the inner query (subquery) first, then build the outer query on top of that. None
Use a subquery to count distinct field values Write a subquery selecting all the DISTINCT records, and then query those results with a COUNT(*): SELECT COUNT(*)
FROM (
SELECT DISTINCT *
FROM inspections) AS temp
Include the distinct count in a query Create a new column that has a count of how many times each establishment appears in the table: SELECT a., b.estab_count
FROM inspections AS a
INNER JOIN (
SELECT estab_name, COUNT(
) AS estab_count
FROM inspections
GROUP BY 1) AS b
ON a.estab_name = b.estab_name

This formatting should make the SQL statements easier to read and understand. Let me know if you need any further adjustments or assistance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment