Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Databases Reporting with SQL Aggregate and Numeric Functions Counting Results

Taylor Han
Taylor Han
2,406 Points

Counting with SQL

SELECT COUNT(*) FROM books WHERE genre = "Science Fiction" AS scifi_book_count;

Trying to return the number of books in a database that are in the "Science Fiction" genre, and alias it. Tried a few different variations but not sure what I'm doing wrong.

Thanks!

1 Answer

Steven Parker
Steven Parker
218,829 Points

In SQL syntax an alias must immediately follow the item it applies to (in this case the "COUNT" function).

SELECT COUNT(*) AS scifi_book_count FROM books WHERE genre = "Science Fiction";
Taylor Han
Taylor Han
2,406 Points

Ah ok thank you! That has been a confusing thing with alias'.

Is there anywhere we can find a good explanation of what order each operation needs to go in. I find that very confusing and frustrating. Is there maybe a rule of thumb or something?

Steven Parker
Steven Parker
218,829 Points

I think what you want is often called a "cheat sheet". Here's a couple I found with a quick search:

However, despite the term "basics", these both include several things that probably have not been covered yet.