How do you count number of Columns using COUNT function?

Hi All!

This is a very basic question but how do I get a count of columns using COUNT function? Let’s say I have imported a very large data table and want to know how many different columns it has without manually counting them. And perhaps later on I want to filter columns with specific data (say only containing text). How do I go about it?

Thanks!
A

Hi @css2389480521, welcome to the forums!

This is actually not a basic question as far as SQL is involved. It is actually not covered in the Codecademy curriculum.

If you are using SQLite, you can use PRAGMA functions to find this info. See here for documentation.

To find the number of columns for a table (let’s call this table tracks) you would use this query:

SELECT COUNT(*)
FROM pragma_table_info('tracks');

If you want to find the names of the columns that contains strings, you would use this query:

SELECT name
FROM pragma_table_info('tracks')
WHERE type LIKE 'NVARCHAR%';

From there you can just SELECT those columns FROM tracks if you want.

As far as selecting only the columns that contain strings within your main SELECT statement (filtering by type), I suggest looking on Google and doing some research if it is important to you, as that is what I would have to do to answer your question.

Hope this helps. Happy coding!

1 Like