I need help please !?

Describe what does the following pseudo code do:

################################################################

articles = db.query(“select title, author_id from blog_articles where is_visible = 1”).fetch_all()

authors = db.query(“select id, full_name from authors”).fetch_all()

for (article in blog_articles) {
print "Article Title: "+article.title
for (author in authors) {
if (article.author_id == author.id) {
print "Article Author: "+author.full_name
}
}
}

################################################################

How would you improve it?
Your Solution

select a.title, a/author_id,b.full_name
from blog_articles a,
          authors b
 where a.is_visible = 1
    and a.author_id = b.author_id

Thanks so much but what the pseudo code do in these situation? do u hapen to know

It does what the SQL I posted does… it returns a list of author information by joining two tables.
Your example does a lot of extra work by doing the JOIN one record at a time
While mine does it the correct way by using the power of SQL

If you do not understand SQL then THAT is where I would suggest your start your research.

How many marks is it worth? :slight_smile:

It gets a list of articles, and a list of authors.

Work through every article one by one.
Every time you get a new article, look through all of the authors and if you find a match, print the result
It wastes time looking at all the authors, even if it has found a match.
It wastes time by not joining the tables together in the way that Dave suggests.

But there may not be an author in the table for every article. The author may be duplicated, in which case you will get several printouts per article.

If you want all articles anyway…

select a.title, a/author_id, b.full_name from blog_articles a, left outer join authors b on a.author_id = b.author_id where a.is_visible = 1

…Giving a full list of articles, and letting you choose whether to report on one that lacks an author, if you choose.