Introspection
Learn SQL introspection with practical examples and guided drills on dbSyntax.
Start with IntrospectionIntrospection
Ask the database what's in it. List tables, describe a table's columns, find which columns have foreign keys, list databases and schemas. Us...
List all tables in a database
Short answer: SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' works on Postgres / MySQL / Snowflake / BigQuer...
Describe a table — show columns, types, and constraints
Short answer: SELECT * FROM information_schema.columns WHERE table_name = 'orders' works on every major engine. Most engines also have a met...
List databases and schemas
Short answer: SELECT schema_name FROM information_schema.schemata lists schemas on most engines. Listing databases is engine-specific. Most ...
Find foreign keys for a table
Short answer: join information_schema.key_column_usage with information_schema.referential_constraints to get every foreign key with its sou...
INFORMATION_SCHEMA recipes
INFORMATION_SCHEMA is the ANSI-standard set of read-only views that describe every database object: tables, columns, constraints, routines, ...