using row from first part of SQL union in second part - sql

To implement a multiple choice quiz I want to select an answer from my answers table (the "correct" answer) and two "incorrect" answers. Can I do it in one query. I feel I should be able to but I'm not quite getting it. Here is what I have so far:
SELECT correct.answer
FROM (
SELECT answer
FROM answers
ORDER BY random()
LIMIT 1
) correct
UNION
SELECT answer
FROM (
SELECT DISTINCT answer
FROM answers
WHERE answer != correct.answer
ORDER BY random()
LIMIT 2
);
The database engine is SQLite 3 which gives me:
Error: no such column: correct.answer
I can do it with two separate queries but like I said, one should be possible no?

Related

Set random seed in bigquery

I've seen these two questions mention the same issue. The answers are almost an year old, and wondering if any more updates were given in BQ - I could not find any concrete answers in the documentation.
I'm trying to do repeated sampling and would like consistent results. This is important for me.
The solution provided in this question, does not provide consistent results.
Here is my code
SELECT
*
FROM (
SELECT
*,
ROW_NUMBER() OVER() as incremental_number
FROM (
SELECT
*
FROM
Table1 as cmd
WHERE
Member NOT IN (
SELECT
Member
FROM
table2
WHERE
Idx = ‘6’
)
) as t
WHERE
MOD(ABS(FARM_FINGERPRINT(TO_JSON_STRING(t))), 10) < 5
)
ORDER BY Member
Maybe just the order of rows is different? Try to sort them.

SQL - how to query two tables and return the results as one?

Long story short, I have this website where "Parent" level comments are stored in a separate table than their replies, known as "Child" comments.
To produce a list of all Recent Comments, I've been doing something like this:
SELECT
TOP 10
PC.ParentCommentText,
PC.ParentCommentID,
PC.ParentCommentTimeStamp
FROM
ParentComments AS PC
We'll call that Query1.
Query2 is the same:
SELECT
TOP 10
CC.ChildCommentText,
CC.ChildCommentID,
CC.ChildCommentTimeStamp
FROM
ChildComments AS CC
Then I use a Union and query the queries so I can loop through all the results at once and display a mixed list of most recent.
SELECT * FROM Query1
UNION
SELECT * FROM Query2
ORDER BY ParentCommentTimeStamp DESC
My problem starts with the first two queries - I can only return a certain number from either table to start. So if some thread runs away and becomes a big discussion, all the slots for "Child" comments are filled, and you might still see "Parent" comments that are hours or days old, despite them not being anywhere near Recent.
It's been awhile since I've done anything requiring deep knowledge of SQL. I know there's a way to select Parent and Child comments as one, and just grab __ number of the results, despite which table they originated from.
Help?
Use union all to combine the query results and order by later to get the top n rows.
SELECT TOP 10 WITH TIES *
FROM (SELECT ParentCommentText,ParentCommentID,ParentCommentTimeStamp as comment_timestamp
FROM ParentComments
UNION ALL
SELECT ChildCommentText,ChildCommentID,ChildCommentTimeStamp
FROM ChildComments
) T
ORDER BY comment_timestamp DESC

MS Access select distinct random values

How can I select 4 distinct random values from the field answer in MS Access table question?
SELECT TOP 4 answer,ID FROM question GROUP BY answer ORDER BY rnd(INT(NOW*ID)-NOW*ID)
Gives error message:
Run-time error '3122': Your query does not include the specified
expression 'ID' as part of an aggregate function.
SELECT DISTINCT TOP 4 answer,ID FROM question ORDER BY rnd(INT(NOW*ID)-NOW*ID)
Gives error message:
Run-time error '3093': ORDER BY clause (rnd(INT(NOWID)-NOWID))
conflicts with DISTINCT.
Edit:
Tried this:
SELECT TOP 4 *
FROM (SELECT answer, Rnd(MIN(ID)) AS rnd_id FROM question GROUP BY answer) AS A
ORDER BY rnd_id;
Seems to work sofar..
I suggest:
SELECT TOP 4 answer
FROM question
GROUP BY answer
ORDER BY Rnd(MIN(ID));
I don't think the subquery is necessary. And including the random value on the SELECT doesn't seem useful.
I've creted a simple quiz application 2 years ago, and this is the query that I use to get a random question from the table.
SELECT TOP 4 * FROM Questions ORDER BY NEWID()

Oracle SELECT - alias of one column as an input to another [duplicate]

This question already has answers here:
Oracle: Using Pseudo column value in the same Select statement
(3 answers)
Closed 9 years ago.
Folks I found similar but not exact questions on this forum - pardon me if i have not done enough searching for them. This is my question..in Oracle
select ( t.value*2) as inst2, (inst2 * 3) as inst3
from table t;
the thinking behind is if f() = t.value*2 is an expensive call, then we do not need to make that twice..or is there an alternative query structure i could use (I am trying to achieve this in CTAS)
thanks in advance.
Another option:
with cte as (
select t.value*2 as inst2
)
select
cte.inst2,
(cte.inst2*3) as inst3
from cte
This is actually the same as in bluefeet's reply, but I would consider it easier to understand with the with-syntax.
If you want to use an alias in the second calculation, then you will want to use a subquery:
select inst2,
(inst2 * 3) as inst3
from
(
select t.value*2 as inst2
from table t
)

which query is more preferable and why

i am trying to teach myself SQL and of course I would like to follow best practices.
I have created two querys to find the latest record :
select * from AppSurvey where
DateLastUsed >= ( SELECT MAX(DateLastUsed) FROM AppSurvey)
and
select top 1 * from AppSurvey order by DateLastUsed desc
is one of these methods more efficent than the other or does it really matter
There is a similiar post on this site to what you are trying to get at.
For autoincrement fields: MAX(ID) vs TOP 1 ID ORDER BY ID DESC
The preferred answer seems to be: "In theory, they will use same plans and run almost same time"
The first one could get more than one row, if your DateLastUsed column isn't unique.