How can I get the count in SQL?

Let’s learn how can I get the count in SQL. The most accurate or helpful solution is served by Stack Overflow.

There are ten answers to this question.

Best solution

How can I get the rows along with row count in SQL?

I have written the following query: select *, count(pk_id) as row_count from employee group by pk_id But I am not getting 1 as the row_count value for every column. How can I get the total number of rows returned as the result of the query? Can someone please help? And one more thing is I don't want to write any subquery :(


Have you considered either just counting the rows as you receive them in whatever's consuming this result...

Read more

Deviprasad Das at Stack Overflow Mark as irrelevant Undo

Other solutions


More information please, tables with definitions, what database are you using? With this information...

Read more Mark as irrelevant Undo

How can I count unique items in sql?

I got help from here: How can I use Count(*) twice in one sql statement? for a query and from that I got this query: SELECT userID, COUNT(beerName) as NumberofBeers FROM uniqueBeers WHERE userID IN (3, 6) GROUP BY userID ORDER BY `uniqueBeers`.`userID...


SQL supports the DISTINCT keyword inside some aggregate functions. SELECT userID, COUNT(DISTINCT beerName...

Read more

Bill Karwin at Quora Mark as irrelevant Undo

How can I use Count(*) twice in one sql statement?

I have a database with a list of items and users for each item. Different users can both have the same item. If I want to get a count of the items for a single person I can do this: $query = "SELECT beerBrewery, COUNT(*) as brewery FROM uniqueBeers...


Are you looking for getting grouped counts or individual kinds counts not associated with a group by...

Read more

Andrew Hansen at Quora Mark as irrelevant Undo


Ok this question should be relatively easily for someone who knows their SQL. It has to do with COUNT and DISTINCT I'm using Microsoft SQL Server. All right I have a table that looks like so: SalesTable SalesPerson Department Item Qty Price SaleDate...


Hi, Mandeep: I don't think there is any simple syntax that you are overlooking. The COUNT( DISTINCT...

Read more

mdeeps-ga at Google Answers Mark as irrelevant Undo

How to query XML like SQL?

I'm looking for a software tool that will ingest a large XML file, and allow me to write sql-like queries against it for both summary reporting, as well as detailed reports. I've been tasked with trying to extract some statistical information about data...


Didn't read the original poster's subcomment later: My whole goal is to be able to use SQL, which I...

Read more

nomisxid at Ask.Metafilter.Com Mark as irrelevant Undo

Is there a way to use a While within a SQL Union query?

I have the following SQL query. I need to have a WHILE loop in the second half. I can't break the query into two queries, because the queries are in a SSRS report, and need to be in the same table. I get an error where the WHILE is. Is there any way...


Look at Recursive CTE (Common Table Expressions), which is supported from SQL Server 2005.

Read more

sweetie_... at Yahoo! Answers Mark as irrelevant Undo

How do I get all the auto-incremented IDs for multiple row inserts using a single insert statement?

I posted the same question on Stack Overflow also. I have a table named settings(which stores user settings) and I have  to insert multiple settings for each user. Initially, I had executed a  separate insert statement for each setting, but having felt...


You can just use mysql_insert_id(). It will return the first ID of the bunch that was inserted into...

Read more

Geoffrey Reemer at Quora Mark as irrelevant Undo

How to get an average per day on MS Access using SQL?

Hi, I am using access SQL to create a query, and this is my code: Select Weekday (date_Entered) AS [Day of Week], count(*) AS [Total Users], -int(-count(*)/DateDiff("ww", date_entered, now())) AS [average] FROM Pool_Usage GROUP BY Weekday ...


I'm not sure what you're trying to accomplish with your 'average' computation, but try using subqueries...

Read more

S at Yahoo! Answers Mark as irrelevant Undo

Do some people get irritated when other people refer to a "SQL database" instead of a "SQL Server database"?

SQL is a language, SQL Server is a product, right? (Sorry for the awkward phrasing, I tried to do "does anybody else get irritated when..." but Quora won't allow it.) So, my question is not really whether some people get irritated, but whether...


I regularly get upset when people say "SQL" and mean "MS SQL Server".

Read more

Joachim Pense at Quora Mark as irrelevant Undo

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.