Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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
231,236 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
231,236 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.