Simple definition: query or sub-query? - sql

I have seen sources saying that an SQL statement such as
SELECT first_name, last_name, subject
FROM student_details
WHERE games NOT IN ('Cricket', 'Football');
is an example of a subquery, but is it not a simple query? I was under the impression that subqueries demand a second call of SELECT, is this correct?

A subquery is a query within a query - your example is just a query.
Your source, http://beginner-sql-tutorial.com/sql-subquery.htm, is incorrect in some ways, I think.
This is a query that contains a subquery:-
USE AdventureWorks2008R2;
GO
SELECT Ord.SalesOrderID, Ord.OrderDate,
(SELECT MAX(OrdDet.UnitPrice)
FROM AdventureWorks.Sales.SalesOrderDetail AS OrdDet
WHERE Ord.SalesOrderID = OrdDet.SalesOrderID) AS MaxUnitPrice
FROM AdventureWorks2008R2.Sales.SalesOrderHeader AS Ord

This statement contains a subquery:
Select First_Name, Last_Name, Subject
From Student_Details
Where GameID not in (Select GameID from Games where RequiresHelmet = 1)
Yours does not.

Related

Is there an elegant way in sqlite to select a single column in a self join?

SELECT mindate.rID
FROM
(SELECT rID, mID, count(mID), min(ratingDate), stars
FROM rating
GROUP BY rID, mID
HAVING count(mID) >1)
AS mindate,
(SELECT rID, mID, count(mID), max(ratingDate), stars
FROM rating
GROUP BY rID, mID
HAVING count(mID) >1)
AS maxdate
WHERE mindate.stars > maxdate.stars
This is an example query that doesn't work, because I can only select either "mindate.rID" or "maxdate.rID" or it gives me an "amiguous column name - error", eventhough either could be the entry I need. The only way I see to solve it is do use "SELECT *", but there must be a more elegant way to solve it, isn't there?
For SQLite you can select both columns if they are qualified by the table's name/alias:
SELECT mindate.rID, maxdate.rID
If you face such a problem I suspect that it has nothing to do with SQLite but with the tool or the programming language that you use to run this query.
In any case it's a good practice not to have in the results 2 columns with the same name so you should alias them:
SELECT mindate.rID minID, maxdate.rID maxID
See a demo.
If what you want is to return only one of the 2 columns based on some condition then use a CASE expression:
SELECT CASE WHEN <condition> THEN mindate.rID ELSE maxdate.rID END

How to write Relational Algebra

Here the relation: WORKS(emp_name, company_name,salary)
Q. Write an expression Relational Algebra to find the company name that has the highest number of employee.
I tried to solve it in many ways but not finding the correct way.
Here is a query which should work across most RDBMS:
SELECT company_name
FROM WORKS
GROUP BY company_name
HAVING COUNT(*) = SELECT MAX(empCount) FROM
(
SELECT COUNT(*) AS empCount
FROM WORKS
GROUP BY company_name
) t
If you are using MySQL, SQL Server, or any database which has a LIMIT keyword (or something like it), then the query gets easier:
SELECT company_name, COUNT(*) AS empCount
FROM WORKS
GROUP BY company_name
ORDER BY empCount DESC
LIMIT 1

postgis postgres count and group by column for ST_Distance function

This SQL produces the following:
SELECT city FROM travel_logs ORDER BY ST_Distance(travel_logs.start_point, ST_GeographyFromText('SRID=4326;POINT(101.652506 3.167610)'))
"Tshopo"
"Tshopo"
"Mongala"
"Haut-Komo"
This SQL produces the following:
SELECT city, count(*) AS count FROM travel_logs GROUP BY travel_logs.start_point, city ORDER BY ST_Distance(travel_logs.start_point, ST_GeographyFromText('SRID=4326;POINT(101.652506 3.167610)'))
"Tshopo";1
"Tshopo";1
"Mongala";1
"Haut-Komo";1
Basically, I want the result like this that groups by city and the number of times same city occurs. something like this
"Tshopo";2 <--- its summed up correctly
"Mongala";1
"Haut-Komo";1
Im not an expert on joins, subquery, would that help ? Thanks in advance.
this worked for me:
select city, count(*) as count
from
(SELECT city FROM travel_logs ORDER BY ST_Distance(travel_logs.start_point, ST_GeographyFromText('SRID=4326;POINT(101.652506 3.167610)'))
) as subquery_travel_logs_nearest group by city
Simple, plain SQL without a sub-query:
SELECT city, count(*)
FROM travel_logs
GROUP BY city
ORDER BY ST_Distance(start_point,
ST_GeographyFromText('SRID=4326;POINT(101.652506 3.167610)'));

Select In query how to order in where clause racle

I have two tables.
This is my first query
select teacher_id from teacher a , stats s where s.type=25 and a.teacher_id=s.teacherP_id order by s.count desc;
My second query
select *from teacher where teacher_id IN (select teacherP_id from stats where type = 25);
In my java class, I am using hibernate
if i use first query, i can take teacher table coz i am just retuning teacher_id if i use select * i am gettin all fields from stats and teachers.
if i use second query i cant order them by count field.
How can i get just with one query all fields of teacher and ordered by stats count field.
Please try, if you could the following query:
select a.* from teacher a , stats s where s.type=25 and a.teacher_id=s.teacherP_id order by s.count desc;
First, you should learn explicit join syntax. If you are learning SQL, a simple rule: never use commas in the from clause.
Second, you can select all the columns from a table using <table alias>.*. The following is the query you want:
select t.*
from teacher t join
stats s
on t.teach_id = s.teacherP_id
where s.type = 25
order by s.count desc;

How to optimize an SQLite3 query

I'm learning SQLite3 by means of a book ("Using SQLite") and the Northwind database. I have written the following code to order the customers by the number of customers in their city, then alphabetically by their name.
SELECT ContactName, Phone, City as originalCity
FROM Customers
ORDER BY (
SELECT count(*)
FROM Customers
WHERE city=originalCity)
DESC, ContactName ASC
It takes about 50-100ms to run. Is there a standard procedure to follow to optimize this query, or more generally, queries of its type?
In the most general case, query optimization starts with reading the query optimizer's execution plan. In SQLite, you just use
EXPLAIN QUERY PLAN statement
In your case,
EXPLAIN QUERY PLAN
SELECT ContactName, Phone, City as originalCity
FROM Customers
ORDER BY (
SELECT count(*)
FROM Customers
WHERE city=originalCity)
DESC, ContactName ASC
You might also need to read the output of
EXPLAIN statement
which goes into more low-level detail.
In general (not only SQLite), it's better to do the count for all values (cities) at once, and a join to construct the query:
SELECT ContactName, Phone, Customers.City as originalCity
FROM Customers
JOIN (SELECT city, count(*) cnt
FROM Customers
GROUP BY city) Customers_City_Count
ON Customers.city = Customers_City_Count.city
ORDER BY Customers_City_Count.cnt DESC, ContactName ASC
(to prevent, like in your case, the count from being computed many times for the same value (city))