Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am learning sql at the moment and i come across exist condition. What i know clearly is that and exist checks for the result of the sub query and if the result is TRUE outer query runs and if it is false outer query does not run.
But i am unable to understand is that how where clause fits in all this and how does it relates to outer query.
Please also explains to me what exactly happens after exist condition checks to TRUE.
Here is an example on how EXISTS relates with the outside query:
SELECT *
FROM classes a
WHERE EXISTS (
SELECT 1
FROM student b
WHERE b.a_id = a.id -- Here you relate inner query with outside query
)
You want to get the records in classes where there are at least one record in table students with a foreign key of the id in table classes.
You relate them in the WHERE of the inner query.
If there are no students in that class, the outside query will not return that class.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I want to make a report from 3 tables like this
SELECT transaksi.id_transaksi,transaksi.waktu_pengiriman,
pengirim.nama_pengirim,penerima.nama_penerima,
penerima.alamat_penerima,transaksi.harga,transaksi.status_pengiriman
FROM transaksi,pengirim,penerima
WHERE transaksi.waktu_pengiriman BETWEEN '2020-11-01' AND '2020-11-30'
I saw this answer on this forum that telling me I could SELECT 3 tables directly like that but the output unexpectedly duplicated so much. This is how it looks like
You are cross joining everything --- you want left joins... I'm guessing what the joins are but it would look something like this:
SELECT transaksi.id_transaksi,
transaksi.waktu_pengiriman,
pengirim.nama_pengirim,
penerima.nama_penerima,
penerima.alamat_penerima,
transaksi.harga,
transaksi.status_pengiriman
FROM transaksi
LEFT JOIN pengirim ON transaksi.id_pengirim = pengirim.id
LEFT JOIN penerima ON transaksi.id_penerima = penerima.id
WHERE transaksi.waktu_pengiriman BETWEEN '2020-11-01' AND '2020-11-30'
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
displays all data with certain field types, from the results of the query ...
I do queries in three tables, and only show data that has relations only, whereas I want to display everything even though there is no relation ...
the data that I have
I want to display the data as below
You need to use left join in this case
select a.position,c.nama from posisi a,kanaikan_posisi b,bana c
where a.id_position = b.id_position (+) and c.id_karyavan (+) = b.id_karyavan
order by a.id_position
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I read somewhere on Internet that using TOP(MSSQL) or LIMIT(MySQL) in your query is best practice.
He explained: If database has millions record and if you use limit then database algorithm will stop filtering other data when it gets record you requested in LIMIT or TOP.
My question is when you fetch record using WHERE condition with ID, So LIMIT or TOP does make any difference as there'll be unique id in database.
PK is applied on that column
SELECT TOP 1 *
FROM TABLE_MASTER
WHERE ID = 10`
OR
SELECT *
FROM TABLE_MASTER
WHERE ID = 10 LIMIT 1`
If this question already asked Please give me link as I was unable to find stackoverflow thread.
If you have a WHERE clause picking a specific row by a unique id, then the query is already restricted. It will scan only the single row matching the specific value. There is no benefit to using TOP or LIMIT in this case.
If someone says to you, "feature X is best practice" that doesn't mean you should use feature X even when it makes no difference.
Using TOP or LIMIT is useful if you have no condition in the WHERE clause, or a condition that would match a very large number of rows. Instead of returning thousands (or even millions) of rows you don't need, you can restrict the quantity of rows.
If there is any chance that ID is not unique there is the possibility that more than one record could be found, having no LIMIT or TOP statement could mess up your code if you only expect one record. As such, it usually doesn't hurt to put the LIMIT / TOP statement in there just in case. If the ID is already a unique PK it won't make any difference on an efficiently coded database engine (aka pretty much all of them).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to SQL, have read many sources and it just tells me to use HAVING instead. Is there a more in-debth solution or reason to the alternate?
Also, is it not an an error to use an aggregate function in a HAVING clause? How is this different than using a one in a where clause?
Conditions applied in the WHERE clause refer to the values in the rows of the table(s) prior to the application of the GROUP BY clause, and conditions in the HAVING clause apply to the result set post-GROUP-BY.
Hence you can reference GROUP BY columns or aggregations in the HAVING clause, but not columns for which the values are not guaranteed to be unique post-GROUP BY. In the WHERE clause, the result set of the GROUP BY is not available, so any row vaue can be referenced but no aggregation results can be.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have the following two MySQL tables for emails...
mail_emails
Columns: id, folder, user
mail_folders
Columns: id, name, user
I want to use only a single MySQL query to fetch the folder names and the number of emails per folder. Presuming the user id is 1, what would be the most efficient approach to this goal?
I am not exactly sure of your database structure. But this should give you an idea.
SELECT *, count(e.folder) totalMail FROM mail_folders f INNER JOIN mail_emails e on e.folder = f.name WHERE f.user = '1' GROUP BY e.folder
I am assuming that the name field in the mail_folders table is the same value that will be stored in folder field of mail_emails table.