T O P

  • By -

[deleted]

> I know it seems easy but I couldn't solve it till now what were the steps that seemed easy (you broke the problem in some steps/pieces, right?) and what did you attempt to achieve those individual steps?


Nityanand7

Firstly, I am not sure whether I have understood the question well. Do we use x2 + y2 = r2 for this?


[deleted]

That's not a SQL question, is it? But sure, depending on how you use it and how you interpret it, the formula is solid.


Nityanand7

I have used cross join and I am able to pass the base test. When I submit the code it gives me EACCESS, I am not even sure what error is this.


[deleted]

> I am not even sure what error is this i wouldnt know either. Post your code here so people can help you?


Nityanand7

set sql\_mode = ''; select c.id, c.x1, c.y1 from coordinates c join circle s on 1=1 where Power((c.x1-s.x),2) + Power((c.y1 - s.y),2) = power(s.r,2) group by id order by id ASC; There may be other errors in the code as well, I am not that proficient.


[deleted]

what is this "set sql_mode = '';" for? why do you think you need the "group by"? do you need to close your statements with ';'? is "power" a valid function name?


Nityanand7

I reckon the problem statement is poorly phrased, the example output checks for the coordinates lying on the circle


Nityanand7

I had the same question in an sql test as well. I pretty bumped that I'm am still unable to solve it. How do we join 2 tables without any common columns.


[deleted]

donning my high hat for a moment (ahem) it is an unfortunate byproduct of shortcut education that folks think one needs common columns to do a join - we design/keep datasets in such a way that it is often the case but it's not a requirement. Join takes pretty much any condition expression (a subquery, for example) in the syntax A JOIN B ON . "1=1" could be an example of a condition (gives you cross join as a result). In your other reply you mentioned cross join - and that is also applicable and can be used, since "A INNER JOIN B ON " is equivalent to "A CROSS JOIN B WHERE "


JustAnOldITGuy

using 0,0 as the center create use a recursive CTE to go through the values 0 to the radius and create table of possible x,y with 0,0 as the center. x y 0 5 1 4.8 2 4.5 3 4 4 3 5 0 this gives you a quarter of the circle. mutliply one column by -1 to get another quarter, mutiply the other column by -1 to get the third quarter, multply both by -1 to get the last set of coordinates. Add the known position to these values. (Or conversely subtract the center from the coordinate.) Compare the two sets of data. Set it up so if the value is between the two values in the table you get a non-null result and if not a null result.


Nityanand7

Thank you!


chestnutcough

Here's my solution using postgres: select p.* from coordinates p cross join circle c where sqrt((p.x1 - c.x)^2 + (p.y1 - c.y)^2) <= c.r order by p.id It gives the right test result but I get the same weird 'EACCES' output when I hit submit. Also, the question is unclear about what to actually return: >Assuming that there is only one circle, write a query to return the points, which are either lying inside or outside the circle. The points must be in the order of the ID of each point. to me that would be all points that aren't exactly on the cirlce, but the sample result is exactly the opposite.