SQLite "in" expression on multiple columns - sqlite

I have such database query, which works well in MySQL:
select authors.name, authors.surname, books.name, publishers.name, copies.comment
from authors, books, publishers, copies
where (authors.id, books.id) in (select authorship.author_id, authorship.book_id from authorship)
and (books.id, publishers.id, copies.publish_id) in (select publishment.book_id, publishment.publisher_id, publishment.id from publishment);
In SQLite database I've got such error on part "where (authors.id, books.id) in":
Error: near ",": syntax error
Is it possible to make such type of query in SQLite (maybe - with different syntax)?
PS: I know that using joins is better for those case, but I want to know if it is possible for general knowledge.

I know that using joins is better for those case, but I want to know if it is possible for general knowledge.
No, this is not possible. Use explicit joins.
SELECT
a.name, a.surname, b.name, p.name, c.comment
FROM
authors a
INNER JOIN authorship ab ON ab.author_id = a.id
INNER JOIN books b ON b.id = ab.book_id
INNER JOIN publishment p ON p.book_id = b.book_id
INNER JOIN copies c ON c.publish_id = p.id
(And by the way, you should do the same for mySQL)

Related

Using querys to get information from different tables

I am learning SQLite and I'm using this database to learn how to correctly use querys but I'm struggling specially when I have to use data from multiple tables to get some information.
For example, with the given database, is there a way to get the first name, last name and the name of songs that every customer has bought?
All you have to do is a simple SQL SELECT query. When you say you're having trouble getting it from multiple tables, I'm not sure if you're trying to get the data from all of the tables in one single query, as that is not necessary. You just need to have multiple instances of the SELECT query, just for different tables (and different column names).
SELECT firstName, lastName, songName FROM table_name
You have to study about JOINS:
select
c.FirstName,
c.LastName,
t.Name
from invoice_items ii
inner join tracks t on t.trackid = ii.trackid
inner join invoices i on i.invoiceid = ii.invoiceid
inner join customers c on c.customerid = i.customerid
In this query there are 4 tables involved and the diagram in the link you posted, shows exactly their relationships.
So you start from the table invoice_items where you find the bought songs and join the other 3 tables by providing the columns on which the join will be set.
One more useful thing to remember: aliases for tables (like c for customers) and if needed for columns also.
You need to use joins to get data from multiple tables. In this case I'd recommend you using inner joins.
In case your are not familiar with joins, this is a very good article that explains the different types of joins supported in SQLite.
SQLite INNER JOINS return all rows from multiple tables where the join
condition is met.
This query will return the first and last name of customers, and the tracks they purchased.
select customers.FirstName,
customers.LastName,
tracks.name as PurchasedTracks from invoice_items
inner join invoices on invoices.InvoiceId = invoice_items.InvoiceId
inner join customers on invoices.CustomerId = customers.CustomerId
inner join tracks on invoice_items.TrackId = tracks.TrackId
order by customers.LastName

Simple.Data coalesce operation in select statement

can any one help on translating below T-SQL statement to
Simple.Data query
select isnull(A.SomeValue,B.SomeValue),T.Id
from Table1 T1
left outer join ATable A on T1.AID = A.Id
left outer join BTable B on T1.BId = B.Id
Simple.Data doesn't support coalescing in T-SQL. The standard way to get around this is to put the query in a stored procedure and then use Simple.Data to call the sproc. Details for that can be found enter link description here.

Combining data from two SQL queries

I'm porting my app from Django to ASP.NET Webforms (against my will, but what can we do with the corporate world..), and I'm used to Django generating all my SQL queries so now I need help.
I have 3 tables: proceso,marcador,marcador_progreso
Every proceso has many marcador_progreso, which in turn is the foreign key table to marcador.
So basically the tables look like:
proceso
id
marcador
id
text
marcador_progreso
id
marcador_id
proceso_id
state
For all the marcador_progreso where its proceso_id is the current proceso (from a QueryField in the URL), I need to list its state and it's respective marcador.text.
I've been working with EntityFramework but this is like a double query so I'm not sure how to do it.
I guess it is something that combines the following two statements, but I'm not sure how to do it.
SELECT [state] FROM [marcador_progreso]
SELECT [text] FROM [marcador] WHERE ([id] = marcador_id)
You want to do a JOIN:
SELECT mp.state, m.text
FROM marcador_progreso as mp
INNER JOIN marcador as m
ON mp.marcador_id = m.id
This is an excellent post that goes over the various join types.
You'll want to know about JOINs to call more than one table in your FROM clause. JOIN combines records from two or more tables in a database by using values common to each. There are different types - the SQL example below is an INNER join, which gets only records where both of the tables have a match on the common value. You may want to consider a LEFT join which would get any records that exist for the LEFT table (in this case marcador), even if there are not any matching record in the RIGHT(marcador_progreso ) table.
Pop the below in Management Studio, Play with different joins. Replace the INNER with LEFT, run it without the WHERE.
Read about `JOIN's.
In general, for your new venture of writing your own queries, they all start with the same basic structure:
SELECT (UPDATE,WHATEVER DML statement, etc) (COLUMNS) what you want to display (update,etc)
FROM (TABLE) where those records live
WHERE (FILTER/LIMIT) conditions that must be met by the data
Happy fetching!
SQL:
DECLARE #ProcessoId int
SET #ProcessoId = --1
SELECT m.[STATE],mp.[TEXT]
FROM marcador M
INNER JOIN marcador_progreso MP ON MP.marcador_id = m.id
WHERE proceso_id = #ProcessoId
EF INNER example
var marc = from m in yourcontext.marcador
join mp in yourcontext.marcador_progreso on m.id equals mp.marcador_id
where proceso_id == processoIdvariable
EF LEFT example
var marc = from m in yourcontext.marcador
join mp in yourcontext.marcador_progreso on m.id equals mp.marcador_id into details
from d in details.DefaultIfEmpty()
where proceso_id == processoIdvariable

How to increase performace on a sqlite3 database that uses QT?

I am using QSQlQuery on a sqlite3 database. To fetch a particular item , I was populating the result from 4 different tables. I thought joining the tables would increase the performance/speed and get the result faster. So I joined 2 tables initially but it takes longer time to fetch the data after joining the tables (?)
Any suggestion on how to improve the performance would be really appreciated. Also, I was looking at the http://qt-project.org/doc/qt-4.8/qsqlquery.html and it is mentioned that using setForwardOnly would increase the performance on some databases. Any idea if it would work for SQLite3?
Thanks!
According to this link,
http://sqlite.org/cvstrac/wiki?p=PerformanceTuning
SQLite implements JOIN USING by translating the USING clausing into some extra WHERE clause terms. It does the same with NATURAL JOIN and JOIN ON. So while those constructs might be helpful to the human reader, they don't really make any difference to SQLite's query optimizer.
-I was wrong to join two tables and expect the fetch to be faster. It does not work with SQLite database. Instead using a "where" clause and joining two results directly definitely has some positive impact on the performance.
(example :
select * from A,B where A.id = B.id where A.id = 1; instead of
select * from A left outer join B on A.id = B.id where A.id = 1)
The SQLite translates first statement to second before compiling and you could save on the small amount of CPU time by directly using the second statement

SQLAlchemy+SQLite Left Join Performance Issue

I have two identical queries save for the position of the left join in the from clause. One runs very slow, the other runs very fast (abbreviated for clarity):
--SLOW
SELECT DISTINCT b.id
FROM a LEFT OUTER JOIN b ON a.id = b.id
JOIN c ON a.id = c.a_id
JOIN d ON c.id = d.c_id
WHERE d.value = 9;
--FAST
SELECT DISTINCT b.id
FROM a JOIN c ON a.id = c.a_id
JOIN d ON c.id = d.c_id
LEFT OUTER JOIN b ON a.id = b.id
WHERE d.value = 9;
My problem is that, using SQLAlchemy, I seem to only be able to create the slow version of the query. Specifically, I am using table inheritance and am trying to run the following code:
return session.query(A).\
with_polymorphic(B).\
join(C).\
join(D).\
filter(D.value_id == 9).\
distinct()
In other words, I don't have control over where the LEFT JOIN is being created.
How do I make SQLite and/or SQLAlchemy smarter about this?
I am not aware of the ways to force SA into changing the query. But I would to identify why there is a difference in execution plan for the query. I would assume that if you have indices on the join columns, the order of the JOINs in the query should not matter.
You can use the EXPLAIN statement to check the execution plan. Although you need to get familiar with The Virtual Database Engine of SQLite. Still it might be possible you spot the difference between two execution plans immediatelly and will be able to improve the database performance instead of tricking the SA.
Try to also remove C or D from the query for even easier comparison of execution plans.

Resources