Consider the following tables, what would be an efficient query to return 1 row for each order that has at least 1 child row with a specific warehouse code? I am using SQL Server 2016. Table: Orders …
Tag: subquery
Joining two select statements together with outer join
My query on SQL Server: select s.learners_id, s.cv_student_id, s.first_name + ‘ ‘ + s.last_name student_name, p.program_name, dc.fulldate program_start_date, sd.discount_value, dt….
count() results without using group by
I am attempting something very similar to last example (Using GROUP BY) on this page: https://thecodedeveloper.com/mysql-count-function/ Referring to the following table of data: The following query: Will produce the following output: Except I want to see the number of employees in each department as well as …
Writing a sub-query using a date clause
So basically I’m trying to write something that prints the customer’s name and area code if they have made a booking within the last 6 months of the current date. My code looks like this (it has to be a subquery, not using join) Yet it doesn’t work, but I can get it to work using a join func…
Updating a table in SQL Server with information from another table
I have a “claims” table. Each claim has a client attached to it which is represented by a few letters “ABED”. For some reason, the actual clientID is nowhere to be found in the claims table so I want to fix this. The “Clients” table DOES contain both the code AND clientID. …
MYSQL subquery results columns not as expected
I am trying to build a query that will build a output like this. The output that I go is this I was following the instructions here https://www.mysqltutorial.org/mysql-subquery/ My query is this What I was expecting to happen was the alias ‘patient appt’ to be populated with the data from the nest…
Postgres, split single row into multiple rows
I’ve got a table with columns like: I want to return: I ended out writing a large query using 3 unions like this, but this does not seem right: Is there a way to do this without the mess? Seems like I’m missing something really obvious. Answer In Postgres, you can efficiently unpivot the columns t…
MySQL: Join three tables, comparing two values
First of all, I’m an amateur on SQL. Here it is the example. From this three tables I would like to know who are the teachers that make more money than Mike Table1: Table2: Table3: So far I’ve made this: And I don’t know how to keep going because when I run it, an error appears. My expected …
How to build a query so that I can find all events along with an extra bool column “attending” which is a join on attendees table?
I have a events table, and I have a events_attendees table which has the event_id and user_id. For example, I want to find events around some user (user_id = 1) but also know whether or not they are attending through that additional column. Here’s the query that would get me the pins where the user is a…
PSQL query to insert the records from one table to another based on condition
I have 2 Postgres tables with the following structure: Lets say I have data in the tmp table as below: Now I want to write a psql INSERT INTO query which will copy the data from public.tmp to public.mo table and also perform below condition When a Host has mix values in STATUS column, for eg if HOST: RhelTest…