I am new to SQL, and am using an ORM for the most part (well, a SQL builder, not raw SQL). I have a simple dictionary app in Vercel Postgres version 15 with this sort of schema:
CREATE TABLE words (
id serial PRIMARY KEY,
transcription_count INTEGER NOT NULL,
pronunciation_count INTEGER NOT NULL,
verified BOOLEAN NOT NULL
);
CREATE TABLE transcriptions (
id serial PRIMARY KEY,
word_id INTEGER REFERENCES words,
text TEXT NOT NULL,
system INTEGER NOT NULL,
length INTEGER NOT NULL
);
CREATE TABLE pronunciations (
id serial PRIMARY KEY,
word_id INTEGER REFERENCES words,
text TEXT NOT NULL,
syllable_count INTEGER NOT NULL
);
I want to find all words where the transcription text length is >= 5, for example, and sort on the text, but return the words. Each transcription belongs to a system, and there can be 1 or more transcriptions per system (such as, there are 2 spellings for the word "color/colour" given the "latin" system, as a rough example). Additionally, there can be multiple systems per word (Chinese you have pinyin and Chinese characters, as 2 possible systems, and in the Chinese system you have traditional and simplified as 2 transcriptions within the system, for example).
Here is what I'm thinking.
SELECT id from words
INNER JOIN transcriptions t ON t.word_id = words.id
WHERE t.length >= 5
AND t.system = 1
ORDER BY t.text ASC
Is that the correct type of query? Will that return duplicate words, because of the fact that I'm sorting by the joined table, and there is a 1<>many relationship? I'm unclear how that would work / what kinds of results that would return. Also, as a second query, how could I additionally specify "select only the first transcription matching the request, for each word", so it only sorts by 1 transcription per word?