I have got 2 tables
Student
|---------------------|------------------|
| id | studentname |
|---------------------|------------------|
| 1 | studentA |
|---------------------|------------------|
| 2 | studentB |
|---------------------|------------------|
| 3 | studentC |
|---------------------|------------------|
| 4 | studentD |
|---------------------|------------------|
Chair
|---------------------|------------------|
| id | chairname |
|---------------------|------------------|
| 1 | chairA |
|---------------------|------------------|
| 2 | chairB |
|---------------------|------------------|
| 3 | chairC |
|---------------------|------------------|
I need a query to randomly assign a student to a chair in postgresql. Note that there can be more chairs than students or vice versa. In case there is more student than chairs, students not assigned will have null value in chair.
For the above tables we should have a result like
|---------------------|------------------|
| studentname | chairname |
|---------------------|------------------|
| studentA | chairC |
|---------------------|------------------|
| studentB | chairA |
|---------------------|------------------|
| studentC | chairB |
|---------------------|------------------|
| studentD | NULL |
|---------------------|------------------|
OR
|---------------------|------------------|
| studentname | chairname |
|---------------------|------------------|
| studentA | chairA |
|---------------------|------------------|
| studentB | chairC |
|---------------------|------------------|
| studentC | NULL |
|---------------------|------------------|
| studentD | chairB |
|---------------------|------------------|
Any idea how this can be done in postgresql?
