Say basically have 2 tables. The first holds customers ids as well as their first names. The 2nd one holds user ids as well as their last names, however the rows within this table might exist based on if the user has given their surname or otherwise.

I wish to choose both name and also the surname, but when just the name is available then to simply choose that by itself.

I cant use something similar to this if the 2nd table row does not exist it returns nothing:

$db->query("select firstname.fname, lastname.lname from firstname, lastname where firstname.userid = lastname.userid");


SELECT f.fname, l.lname
FROM firstname f
   LEFT JOIN lastname l
      ON f.userid = l.userid

this can return something similar to:

fname | lname
John  | Doe
Bob   | NULL

where NULL implies that Bob has not got a surname

JOIN is much more performant than cartesian product you're using inside your example since it will not produce all of the possible mixtures of {firstame,lastname} but simply those which will make sense (those with similar userid)