Posts

Showing posts with the label SQL

Capitalize first letter in SQL

concat(ucase(left( column_name , 1)), substring( column_name , 2))

Show column names in alphabetical order

SELECT c . column_name FROM INFORMATION_SCHEMA . COLUMNS c WHERE c . table_name = 'tbl_name' -- AND c.table_schema = 'db_name' ORDER BY c . column_name

Sort SHOW COLUMNS or DESCRIBE TABLE

select COLUMN_NAME as 'Field' , COLUMN_TYPE as 'Type' , IS_NULLABLE as 'Null' , COLUMN_KEY as 'Key' , COLUMN_DEFAULT as 'Default' , EXTRA as 'Extra' from INFORMATION_SCHEMA . COLUMNS where TABLE_NAME = 'table' and TABLE_SCHEMA = 'database' -- add ordering -- order by Type ;

Count number of distinct occurrences

select column_name, count(*) from table_name group by column_name order by count(*) desc;

Find specific column in table

select table_name     from information_schema.columns     where column_name in ('column1','column2')         and table_schema='yourdatabase';