Questions tagged [nested-queries]

379 questions
10
votes
2 answers

Filtering / Querying by the Contents of a List in DynamoDB

I am attempting to filter a DynamoDB query by the contents of a Map contained within a List. Here's an example of the structure I'm dealing with. { 'EventType': 'git/push' 'EventTime': 1416251010, 'Commits': [ { …
Tanaki
  • 2,233
  • 4
  • 27
  • 39
8
votes
3 answers

Efficient way to get max date before a given date

Suppose I have a table called Transaction and another table called Price. Price holds the prices for given funds at different dates. Each fund will have prices added at various dates, but they won't have prices at all possible dates. So for fund XYZ…
Cobus Kruger
  • 7,369
  • 3
  • 56
  • 96
7
votes
3 answers

How to give the output of the first query(which has two values) as the input to the second?

i get 2 names as the output of the first query.... eg: paul,peter now this should be the input for the second query, which has to display paul's and peter's email ids....
abhijithln
  • 103
  • 2
  • 2
  • 9
7
votes
6 answers

Is it always a good practice to use aliases in sql joins or nested queries?

Is it always a best practice to use - Select E.Id,D.DeptName from Employee E join Dept D on E.DeptId=D.Id instead of - Select Employee.Id,Dept.DeptName from Employee join Dept on Employee.DeptId=Dept.Id Other than readability and reducing length…
Vishal
  • 11,379
  • 15
  • 76
  • 122
7
votes
3 answers

Converting a doubly-nested query to a JOIN statement, and other optimizations

I've got this query which is quite specific and returns exactly what I need. As we know, nested queries in mysql can be pretty terrible in terms of runtime. If anyone could help me convert the following code to a JOIN statement, I would really…
Billy Pilgrim
  • 343
  • 2
  • 12
5
votes
3 answers

Nested MySQL Queries

I have been trying to write a query for a public transportation system which will output a list of routes when I input the origin and destination stop numbers. Here is my MySQL Table : mysql> desc…
Shain Padmajan
  • 424
  • 3
  • 9
5
votes
1 answer

Eloquent Nested relations with where clause query returns collection on false condition

I am trying to search for all Rents that have a RentItem with a book title LIKE the given $input. The problem is when the input does not match,I still get a collection returned. The only difference is that the book relation is null instead of a…
5
votes
3 answers

How to optimize a nested query?

Can I somehow join tables and avoid the usage of distinct in the following MySQL query. invited_by_id shows the user id of who invited this user. SELECT user1.id, count(distinct user2.id) AS theCount, count(distinct user3.id) AS theCount2 FROM …
vian
  • 791
  • 2
  • 11
  • 25
5
votes
6 answers

nested query performance alternatives

From performance perspective , is this the best way to write the following query concerning the nested query : SELECT a.meg,a.currency FROM alt6sal a WHERE a.meg_code IN (1,2) AND a.sal_year = (SELECT MAX(ia.sal_year) FROM alt6sal ia WHERE…
Anyname Donotcare
  • 10,000
  • 54
  • 200
  • 355
4
votes
3 answers

Simplify nested case when statement

Below is my current SELECT CASE statement: SELECT CASE WHEN edition = 'STAN' AND has9 = 1 THEN '9' WHEN edition = 'STAN' AND has8 = 1 THEN '8' WHEN edition = 'STAN' AND has7 = 1 THEN '7' WHEN edition = 'STAN' AND hasOLD = 1 THEN 'OLD' WHEN edition…
coala
  • 195
  • 1
  • 4
  • 13
4
votes
2 answers

How to make a nested query?

Have a table users and there is a field invited_by_id showing user id of the person who invited this user. Need to make a MySQL query returning rows with all the fields from users plus a invites_count field showing how many people were invited by…
vian
  • 791
  • 2
  • 11
  • 25
4
votes
2 answers

How to select last 30 days dates in MySQL?

Can I list somehow the dates of last 30 days in MySQL? Not from a table! For example I think about like this: SELECT date WHERE date BETWEEN SUBDATE(NOW(), INTERVAL 30 DAY) AND NOW(); Is that possible?
netdjw
  • 3,842
  • 11
  • 50
  • 101
4
votes
2 answers

How to use a subquery inside another subquery with three tables in mysql?

I have three tables in my database, see the structure below : users uid | fname | date_joined --------------------------------- 1 | john | 2013-08-25 01:00:00 2 | rock | 2013-08-26 01:00:00 3 | jane | 2013-08-27 01:00:00 questions qid |…
Murtaza Malek
  • 303
  • 2
  • 3
  • 11
4
votes
3 answers

MYSQL insert after sub query with count

I'm trying to INSERT some data into a table but only when the subquery COUNT is > 0. This is what I have so far. INSERT INTO users_friends (userId, friendId) VALUES (77, 100) WHERE (SELECT COUNT(id) FROM users WHERE email = 'a@g.com') > 0 Both…
Andre
  • 233
  • 7
  • 24
4
votes
4 answers

Multi SELECT SQL statement

Writing my first nested SELECT statement and I'm hoping someone can help me out. I need to grab the CompanyCode and AgentId from the UniqueAgentIdtoUniqueAgentId table. However, I only need the ones that show up in a query from another table. The…
NealR
  • 8,689
  • 51
  • 142
  • 274
1
2 3
25 26