How to retrieve data type of table column using SQL

I’m working on a database project and need to find out what data types are used for specific columns in my tables. I know I can check this through database management tools, but I want to do it programmatically using SQL queries.

Is there a way to write a SQL query that will show me the data type of a particular column? For instance, if I have a table named users with columns like user_id, username, and created_date, I want to find out if user_id is an integer, username is varchar, and what type created_date is.

I’m seeking a solution that is compatible with standard SQL, but if there are specific methods for MySQL, PostgreSQL, or SQL Server, I would appreciate those too. What is the best way to obtain this column type information?

You can retrieve data types of table columns by querying the INFORMATION_SCHEMA.COLUMNS view, which is compatible with most SQL databases. For example, use the following SQL query: SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'users' AND TABLE_SCHEMA = 'your_database_name'; This will provide you with the data types for user_id, username, and created_date. For PostgreSQL, consider using pg_catalog for additional details, while SQL Server allows joining sys.columns with sys.types for enhanced information. Overall, INFORMATION_SCHEMA is widely applicable and beneficial.

you can also just use DESCRIBE tablename or DESC tablename - works perfectly in mysql. try desc users; and u’ll get all the column info with datatypes instantly. much easier than those lengthy information_schema queries.

Oh cool! which database are u using? some have their own tricks - like Postgres lets you use \d tablename in psql, which is pretty handy. have you tried any of these yet or still looking around?