Tutorial

Query Basics: SELECT and WHERE

The core of any query is the SELECT statement. Usually instead of asking for everything in a row you select specific columns that you're interested in seeing, like this:

select Title, Id, Score, ViewCount FROM Posts;
Run Query

We use the post title to give us nicer output than just a post number. But wait, you might notice, some posts don't have titles:

results of 'Post Basics' query

Remember that the Posts table contains all posts—questions, answers, tag wikis, and more. Not all of those have titles—questions do but answers do not, most notably. We want to restrict this query to show only questions. The SQL WHERE clause does this:

SELECT Title, Id, Score, ViewCount FROM Posts WHERE PostTypeId = 1
Run Query

This produces only questions.

Now notice that we're still getting all the questions on Meta Stack Exchange. That's a lot of data. We can extend the WHERE clause:

SELECT Title, Id, Score, ViewCount FROM Posts WHERE PostTypeId = 1 AND Score >= 25
Run Query

You can use multiple AND and OR operators to build up a WHERE clause with several conditions. We will see more examples in the next section.