I am currently self-learning SQL so for most of you this would probably seem like a simple question (if I expressed it correctly). I have a table ‘orders’ that look like this: What I want to do is count the times that any item from 2 lists of items (as in IN (itmsid1, itmsid2) coexists for all Ord…
Join on TOP 1 from subquery while referencing outer tables
I am starting with this query, which works fine: SELECT C.ContactSys , … a bunch of other rows… FROM Users U INNER JOIN Contacts C ON U.ContactSys = C.ContactSys LEFT JOIN …
T-SQL Union (distinct, not all) and Order by with priority
Assume I have a Tag table (Id int and Name nvarchar(100)) with 3 values: 1 Software 2 Hardware 3 Warehouse Now I want to query with keyword, and prioritize the ones that starts with the keyword over …
Remove matching values by comparing two database tables Codeigniter/Mysql
In my codeigniter model I have this function : public function getUserNo() { $query = $this->db->query(“select userno from Users where active=1”); return $query->result_array(); } …
How to get details for a customer that is both a seller and a buyer
I have a database which contains customers that are both buyers and sellers. the customers can buy and sell houses. I need to get details for the customers separately and still have a connection …
Left join not returning null records
I’am dealing with a pretty basic SQL query, but I cannot understand why the non matching records are not represented with the null values in right table. I have table A and table B with a composite …
Distinct sql based on condition More than two columns
Below is the table structure. Column one contains strings which might be present in more than one row. I want to write a query in such a way column1 value is only repeated once such that row where …
Using inner join instead of subquery
I wrote a query using a subquery, but I wonder if there is any way to write it using only inner join (or other joins) since it’s more efficient. /*2. List the name of the sales rep who serves the …
Excel VBA with SQL : No row returned when Where clause specified
Here below an excerpt of my codes. This is the SQL command : Where the database located : The variable containing the string connection to the DB : The wrkSheet for the result : This command Adding QueryTable as a ListObject returns ZERO occurrence because of Where Clause whereas the SQL is OK. If the Where C…
“group by desc” syntax error on mysql 8.0 which is fine on 5.7
The statement is like SELECT * FROM db.table group by id desc; Would raise an error like 15:02:24 SELECT * FROM db.table group by id desc LIMIT 0, 10 Error Code: 1064. You have an error in …