SQL: counting columns that can have null values - sql

so I have a query that retrieves names of tables and count of columns in each of them.
SELECT Table_Schema, Table_Name, COUNT(*)
FROM Information_Schema.Columns
GROUP BY Table_Schema, Table_Name
HAVING Table_Schema = 'schema';
How can I add to this query count of columns that CAN store NULL values?

You can add a FILTER condition to the aggregate function to only count the rows where the condition is met:
SELECT table_Schema,
table_Name,
count(*) AS total_columns,
count(*) FILTER (WHERE is_nullable = 'YES') AS nullable_columns
FROM information_schema.columns
GROUP BY table_Schema, table_Name
HAVING table_schema = 'schema';

Use the is_nullable column:
SELECT Table_Schema, Table_Name, COUNT(*)
FROM Information_Schema.Columns
WHERE is_nullable = 'YES'
GROUP BY Table_Schema, Table_Name
HAVING Table_Schema = 'schema';
The documentation on information_schema.columns describes the table in full detail, including other columns that might be of interest.
EDIT:
If you want to add the information to the existing query, just use conditional aggregation:
SELECT Table_Schema, Table_Name, COUNT(*), SUM( (is_nullable = 'YES')::int ) as nullable_clumns
FROM Information_Schema.Columns
GROUP BY Table_Schema, Table_Name
HAVING Table_Schema = 'schema';

Related

SQL query return Error "Function "has_column_privilege(oid,smallint,text)" not supported

i have built Join query on redshift, but it doesn't work. Do you know why?
select
table_schema,
table_name,
sizeMB,
tbl_rows
from
(
select
table_schema ,
table_name
from information_schema.columns
where column_name = 'abcdef' -- enter table name here
and table_schema = 'prev'
and table_name not like '%crfg%'
group by table_schema, table_name
) as a(table_schema, table_name)
join
(
select "table" as tablename, size as sizeMB, tbl_rows
from svv_table_info
where "schema" = 'prev'
group by "table", size , tbl_rows
) as b(tablename, sizeMB,tbl_rows)
on a.table_name = b.tablename
and it got Error: "Function "has_column_privilege(oid,smallint,text)" not supported."

Select data for columns that are only in another table

I have two tables, table1 and table2, that have columns common to both.
The queries that obtain the column names of each table are given below,
Query to get columns from first table:
select column_name from information_schema.columns
where table_schema = 'schema1'
and table_name = 'table1';
Query to get columns from second table:
select column_name from information_schema.columns
where table_schema = 'schema2'
and table_name = 'table2';
I need to select data from table2, only the columns which are also in table1.
I do not have a postrgesql intace at the moment but Dynamic SQL is what you need.
The following query will get you the column names that appear in both table 1 and table 2.
select string_agg(column_name, ',') FROM (
select column_name from information_schema.columns
where table_schema = 'schema1'
and table_name = 'table1'
intersect
select column_name from information_schema.columns
where table_schema = 'schema2'
and table_name = 'table2'
)
And you need to build
EXECUTE 'select ' || select string_agg(column_name, ',') FROM (
select column_name from information_schema.columns
where table_schema = 'schema1'
and table_name = 'table1'
intersect
select column_name from information_schema.columns
where table_schema = 'schema2'
and table_name = 'table2'
) || ' from schema2.table2 '
Sory if there any syntax error as writing from mobile app, you can join both result set to get common data.
select column_name from information_schema.columns T2
JOIN (select column_name from information_schema.columns where table_schema = 'schema1' and table_name = 'table1') T1
ON T2.COLUMN_NAME = T1.COLUMN_NAME Where T2.table_schema = 'schema2' and T2.table_name = 'table2';

Postgres - How to retrieve default value in table structure query

I'm trying to retrieve some table's structures with the column default values using this query :
SELECT column_name, is_nullable, character_maximum_length, udt_name, default_value
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'USER'
ORDER BY ordinal_position
But I'm unable to find the correct value to select the default values. I tried "default_value" but it doesn't work...
As documented in the manual the default value is available in column_default
SELECT column_name, is_nullable, character_maximum_length, udt_name, column_default
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'USER'
ORDER BY ordinal_position

Compare two tables and return colum names which are not equal in two tables

I have two Tables which are, TS_SALES and TS_SALES_AUDIT.
Here TS_SALES is parent table and TS_SALES_AUDIT is child table.
when i count the columns in two tables using below code,
1) SELECT count(*) FROM user_tab_columns WHERE table_name = 'TS_SALES';
---------
Count(*)
---------
130
2) SELECT count(*) FROM user_tab_columns WHERE table_name = 'TS_SALES_AUDIT';
---------
Count(*)
---------
40
Can we return the column names which are not equal in both tables?
This should do the trick:
Columns that are in TS_SALES but not in TS_SALES_AUDIT
SELECT column_name FROM user_tab_columns WHERE table_name = 'TS_SALES'
minus
SELECT column_name FROM user_tab_columns WHERE table_name = 'TS_SALES_AUDIT'
Columns that are in TS_SALES_AUDIT but not in TS_SALES
SELECT column_name FROM user_tab_columns WHERE table_name = 'TS_SALES_AUDIT'
minus
SELECT column_name FROM user_tab_columns WHERE table_name = 'TS_SALES'
One method uses aggregation:
select max(table_name) as which_table_name,
max(column_name) as which_column_name
from user_tab_columns
where table_name in ('TS_SALES', 'TS_SALES_AUDIT')
group by column_name
having count(*) = 1;
To get the columns that exist in TS_SALES and not in TS_SALES_AUDIT, you can use the following query:
SELECT COLUMN_NAME FROM user_tab_columns
WHERE table_name = 'TS_SALES'
AND COLUMN_NAME NOT IN
(
SELECT COLUMN_NAME FROM user_tab_columns WHERE table_name = 'TS_SALES_AUDIT'
)
To achieve the reverse, use the following:
SELECT COLUMN_NAME FROM user_tab_columns
WHERE table_name = 'TS_SALES_AUDIT'
AND COLUMN_NAME NOT IN
(
SELECT COLUMN_NAME FROM user_tab_columns WHERE table_name = 'TS_SALES'
)

Finding the specific column (field) name from multiple tables in a database

I have large numbers of tables in my database. I am searching for a column named Country, but don't know which table contains that column. Is there a specific query that will help me to find the name of the table containing this column?
Yes, you can use INFORMATION_SCHEMA.COLUMNS
SELECT DISTINCT
TABLE_NAME
FROM
INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'Country'
SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name LIKE '%ColumnName%'
ORDER BY schema_name, table_name;
Replace ColumnName to your actual column name
select distinct table_schema, table_name from information_schema.columns where table_name = 'Country';
You can find such of information in the INFORMATION_SCHEMA.COLUMNS
USE YourDBName;
SELECT DISTINCT Table_Name
FROM INFORMATION_SCHEMA.COLUMNS
WHERE Column_Name = 'Country';
If we want to find a column name with like operator in more than one tables (ex: 3 tables) then go for below query:
select COLUMN_NAME, TABLE_NAME
from ALL_TAB_COLUMNS
where TABLE_NAME in ('SIL_RLS_UPLOAD_TMP','SIL_CUSTOMER_MST','SIL_PERSONAL_CUSTOMER_MST')
and upper(column_name) like upper('%loan%');
If we want to find a column name with like operator in all tables then go for below query:
select *
from all_tab_columns
where upper(column_name) like upper('%card%');