The list of all the queries

The number of tables based on the number of columns

Query goal: Find how many tables with a certain range of the number of columns there are in the database.
Query type: Sofware measure (Numeric values (software measures) about the database)
Query license: MIT License
Data source: INFORMATION_SCHEMA+system catalog
SQL query: Click on query to copy it

WITH cols AS (SELECT table_type, table_schema, table_name, column_name
FROM INFORMATION_SCHEMA.tables LEFT JOIN INFORMATION_SCHEMA.columns USING (table_schema, table_name) 
WHERE table_schema NOT IN (SELECT schema_name
FROM INFORMATION_SCHEMA.schemata
WHERE schema_name<>'public' AND
schema_owner='postgres' AND schema_name IS NOT NULL)
UNION SELECT 'MATERIALIZED VIEW' AS table_type, nspname, relname, attname
FROM pg_class INNER JOIN pg_namespace ON pg_class.relnamespace=pg_namespace.oid
LEFT JOIN pg_attribute ON pg_class.oid=pg_attribute.attrelid 
WHERE relkind = 'm' AND attnum>=1),
col_num AS (SELECT table_type, table_schema, table_name, Count(column_name) AS nr_of_cols
FROM cols
GROUP BY table_type, table_schema, table_name)
SELECT 
table_type, 
Count(*) FILTER (WHERE nr_of_cols=0) AS zero_cols,
Count(*) FILTER (WHERE nr_of_cols BETWEEN 1 AND 10) AS between_1_10_cols,
Count(*) FILTER (WHERE nr_of_cols BETWEEN 11 AND 25) AS between_11_25_cols,
Count(*) FILTER (WHERE nr_of_cols>25) AS more_than_25_cols
FROM col_num
GROUP BY table_type
ORDER BY table_type;

Collections where the query belongs to

Collection nameCollection description
Find problems about base tablesA selection of queries that return information about the data types, field sizes, default values as well as general structure of base tables. Contains all the types of queries - problem detection, software measure, and general overview
Find problems by overviewQueries that results point to different aspects of database that might have problems. A human reviewer has to decide based on the results as to whether there are problems or not .
Find quick numeric overview of the databaseQueries that return numeric values showing mostly the number of different types of database objects in the database

Categories where the query belongs to

Category nameCategory description
Structure of base tablesQueries of this category provide information about the structuring of base tables at the database conceptual level

Reference materials for further reading

Reference
Factor, P.: SQL Code Smells. Redgate, http://assets.red-gate.com/community/books/sql-code-smells.pdf, last accessed 2019/12/29 (Creating tables as ‘God Objects’)
Dintyala, P., Narechania, A., Arulraj, J.: SQLCheck: automated detection and diagnosis of SQL anti-patterns. In: 2020 ACM SIGMOD International Conference on Management of Data, pp. 2331–2345. (2020). https://doi.org/10.1145/3318464.3389754 (God Table)
Sharma, T., Fragkoulis, M., Rizou, S., Bruntink, M. and Spinellis, D.: Smelly relations: measuring and understanding database schema quality. In: 40th International Conference on Software Engineering: Software Engineering in Practice, pp. 55–64. ACM, (2018). https://doi.org/10.1145/3183519.3183529 (God table)
Blaha, M.R., Premerlani, W.J.: Observed idiosyncracies of relational database designs. In: 2nd Working Conference on Reverse Engineering, pp. 116–125. IEEE, (1995). https://doi.org/10.1109/WCRE.1995.514700 (Multi-class tables)
Delplanque, J., Etien, A., Auverlot, O., Mens, T., Anquetil, N., Ducasse, S.: CodeCritics applied to database schema: Challenges and first results. In: 24th International Conference on Software Analysis, Evolution and Reengineering, pp. 432–436. IEEE, (2017). https://doi.org/10.1109/SANER.2017.7884648 (Too many columns in a table)

The list of all the queries