The Lion Rampant flag is called the Royal Flag of Scotland, having been used by the Kings of Scotland. The first to have done so was Alexander II (1214-1249).
Much older than the St Andrew’s flag it should now only be used by the Queen in her role as the Monarch of Scotland.
Technically it remains an offence for citizens and corporate bodies to fly or wave the lion rampant flag under the 1672 Act of Parliament because it’s not a national flag. However the law is much more relaxed in modern times. This can be traced back to when, in 1935, King George V gave permission to his subjects to wave small lion rampant flags during celebrations of his silver jubilee.
By the same token the Lion Rampant is widely used as a second national flag especially at football and rugby matches.
However a special department, The Court of the Lord Lyon, who are the authority on heraldry in Scotland, are responsible for legal matters relating to the lion rampant flag and matters of Scottish genealogy and state ceremonial duties.
The Court of the Lord Lyon still prevents people and organisations flying the lion rampant flag from flagpoles.
Exceptions are made if other national flags are flown or if the lion rampant flag is part of the decor with other flags.