To write a subquery, you must adhere to certain guidelines. This includes enclosing the subquery in parentheses and placing it in the correct position of the main query. Additionally, the subquery must return a valid result set that can be used by the main query. For instance, if the subquery is used as a condition, it must return a single value or list of values. On the other hand, if the subquery is used as a source of data, it must return a table with the same number and type of columns as the main query. Furthermore, the subquery can reference columns from the main query, but not vice versa. This implies that the subquery is executed before the main query and can take values from the main query as input, yet the main query cannot access values from the subquery as output.