more than one where condition in sql query - sql

I am trying to execute the following query in pgSQL.
select count(id) from master
where act1=true or act2=true
and regn_no>2000;
But the query return all rows who satisfies the 'act1=true or act2=true' condition. It doesn't check act1=true or act2=true.Why?
SQLFIDDLE:
http://sqlfiddle.com/#!9/23b1f/1

because AND priority is higher than OR then sql parse your where clause as
(act1=true) or (act2=true
and regn_no>2000);
you must rewrite your query as follow
select count(id) from master
where (act1=true or act2=true)
and regn_no>2000;

Related

How to achieve the following sql query in influx?

Given a SQL query below with an IN clause, is there a way in influx to achieve the same output produced by this?
SELECT * FROM Suppliers
WHERE Country IN (SELECT Country FROM Customers)
Influxql does not support IN clause with SELECT statement. Refer here:
One option for you is to demoralize your measurement by flagging(adding additional column) the point in measurement-suppliers which has corresponding matching point within measurement-customers.

SAS SQL where and having

select count(*) from tableA having product="abc";
select count(*) from tableA where product="abc";
why the outpur are different from the above statements as both are same?
Is it possible?
WHERE filters the records that go into the calculations. HAVING filters the result rows that are returned.
If you run your first query then SAS will warn you that it is remerging the results with the original data since you are referencing a non summary statistic variable in your HAVING clause. Note that if no original records meet your HAVING clause then you get no observations in your result set. But if ANY records meet your query then you get a separate observation for each observation that meets your HAVING clause, but count is for all observations since none were filtered.
Try this query.
proc sql ;
select 'HAVING',count(*) from sashelp.class having name like 'A%'
union all
select 'WHERE',count(*) from sashelp.class where name like 'A%'
;
quit;
Then change A% to Z% and run it again.

SQL statement with an impossible constant comparison

I have this SQL query:
SELECT * FROM [table] WHERE 2=3;
Obviously, the query will return 0 rows, but my question is as follows. Does the SQL Server engine evaluate the where condition before executing the selection?
In other words, does SQL Server detect the where condition is impossible and return 0 rows automatically?
If you examine the execution plan for a simple query using such a predicate you will see that SQL Server uses a Constant Scan to determine that zero results will return. No indexes or tables are even touched.
SELECT * FROM [Production].[Product];
SELECT * FROM [Production].[Product] WHERE 1=0;
Basically order is given here
This is order of query execution at SQL:
FROM
ON
OUTER
WHERE
GROUP BY
CUBE | ROLLUP
HAVING
SELECT
DISTINCT 10 ORDER BY
TOP

Get count and result from SQL query in Go

I'm running a pretty straightforward query using the database/sql and lib/pq (postgres) packages and I want to toss the results of some of the fields into a slice, but I need to know how big to make the slice.
The only solution I can find is to do another query that is just SELECT COUNT(*) FROM tableName;.
Is there a way to both get the result of the query AND the count of returned rows in one query?
Conceptually, the problem is that the database cursor may not be enumerated to the end so the database does not really know how many records you will get before you actually read all of them. The only way to count (in general case) is to go through all the records in the resultset.
But practically, you can enforce it to do so by using subqueries like
select *, (select count(*) from table) from table
and just ignore the second column for records other than first. But it is very rude and I do not recommend doing so.
Not sure if this is what you are asking for but you can call the ##Rowcount function to return the count of the previous select statement that has been executed.
SELECT mytable.mycol FROM mytable WHERE mytable.foo = 'bar'
SELECT ##Rowcount
If you want the row count included in your result set you can use the the OVER clause (MSDN)
SELECT mytable.mycol, count(*) OVER(PARTITION BY mytable.foo) AS 'Count' FROM mytable WHERE mytable.foo = 'bar'
You could also perhaps just separate two SQL statements with the a ; . This would return a result set of both statements executed.
You would used count(*)
SELECT count(distinct last)
FROM (XYZTable)
WHERE date(FROM_UNIXTIME(time)) >= '2013-10-28' AND
id = 90 ;

What's the execute order of the different parts of a SQL select statement?

What's the execute order of the different parts of a SQL select statement? Such as
distinct
from
order by
group by
having
multiline function(count, avg, max, min...)
top(sql server) or limit(mysql)
other parts
Does the different databases have the same execution order?
Great thanks.
Have a look at
SQL SERVER – Logical Query Processing Phases – Order of Statement Execution
FROM
ON
OUTER
WHERE
GROUP BY
CUBE | ROLLUP
HAVING
SELECT
DISTINCT
ORDER BY
TOP
Also, for some good info see Logical Query Processing
The above answer addresses the question but there is one exception to the above mentioned order
when you have
select top n ............
order by
Then, order by will be executed before select. ( the entries are ordered first and then the top n entries are selected)